If syntax

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
9902468
Forum Commoner
Posts: 89
Joined: Thu Jun 06, 2002 6:39 am
Location: Europe

If syntax

Post by 9902468 »

Can I do or not like this:

Code: Select all

if ( (somethin == true && something_else == false) || (some != "yeah" && i_am_not == jeesus) ){

     // more code
 
}
Essentially I'm just putting one if and one elseif in the same if clause, Is this allowed, The idea should be pretty clear but let's say it once more:

Code: Select all

if ( (many conditions) || (alternative conditions, if first ones didn't add up) ) { 

}
Might be obvious to you but i'm a bit confused now, as i've tried it out and it works, it doesn't, it works... You get the idea...
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

That sort of syntax is fine and is especially useful for things like this:

Code: Select all

<?php
if ($this == 'something' && ($this2 == 'foo' || $this3 != 'bar')) &#123;
    // code
&#125;
?>
So that you don't have to nest if loops.

The code you put is fine but if it's not working as expected it might be an idea to echo all the relevant variables before running the loop, eg.

Code: Select all

<?php
echo '$somethin: '.$somethin;
echo '$something_else: '.$something_else;
echo '$some: '.$some;
echo '$i_am_not: '.$i_am_not;
if ( ($somethin == true && $something_else == false) || ($some != "yeah" && $i_am_not == jeesus) )&#123; 
     // more code 
&#125;
?>
Mac
User avatar
9902468
Forum Commoner
Posts: 89
Joined: Thu Jun 06, 2002 6:39 am
Location: Europe

Post by 9902468 »

Thanks, now I'm free to hunt that bug down :)
Post Reply