webmasters
webmasters

Reputation: 5831

Idea with php logic using operators

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!

Upvotes: 1

Views: 54

Answers (4)

swordfish
swordfish

Reputation: 4995

$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.

Upvotes: 0

Kai Qing
Kai Qing

Reputation: 18833

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
}

Upvotes: 0

Kerrek SB
Kerrek SB

Reputation: 477020

This is just:

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

(Careful with the string comparison.)

Upvotes: 2

Michael Berkowski
Michael Berkowski

Reputation: 270607

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

}

Upvotes: 2

Related Questions