1

I am new to PHP and find it very hard to explain.

I have a PHP navigation script with 5 categories and two variables relevant to my question:

$catname = 'used-cars'; // the same value for all categories

$currentpage; // pages 1 to 5 

index.php of my site has $currentpage == '1'

The issue is that I need a logic that will say:

If $catname IS NOT 'used-cars', do something, BUT, IF $currentpage is equal to 1, even if $catname is 'used-cats' do it anyway

I am thinking of something like this:

if($catname != 'used-cars' && !($currentpage > '1')):

endif;

Hope you can help!

2
  • You mean for both conditions to be used-cars, correct? In your explanation you have used-cats, which makes for another situation entirely. (lots of used cats need homes-- contact your local Humane Society!) Commented Nov 15, 2011 at 3:48
  • Lol Michael, will do, will do:) Commented Nov 15, 2011 at 3:53

4 Answers 4

2

This is merely a single or condition. On the right side, $currentpage === 1 will evaluate to TRUE without regard to the value of $catname. If either part of the condition is TRUE, you'll enter the if () block to execute your code there.

if ($catname !== "used-cars" || $currentpage === 1) {
  // do your thing

}
Sign up to request clarification or add additional context in comments.

Comments

2

This is just:

if (strcmp($catname, 'used-cars') != 0 || $currentpage == 1)

(Careful with the string comparison.)

Comments

0

Alternatively, you could declare it as a boolean first:

$proceed = false;

if($catname != 'used-cars')
    $proceed = true;

if($currentpage == 1)
    $proceed = true;

if($proceed){
    // whatever you want
}

Comments

0
$doflag = 0;
if($catname != 'used-cars') 
{
  $doflag = 1;
} else if($currentpage == 1) {
  $doflag = 1;
}

if($doflag == 1) {
  //do something
}

Basically instead of trying to do everything with the block, use the block to set a flag and use the flag to do something.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.