if ___ and ___ { }

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

Bennettman
Forum Contributor
Posts: 130
Joined: Sat Jun 15, 2002 3:58 pm

if ___ and ___ { }

Post by Bennettman »

In "if" functions how can I make it so more than one condition needs to be met before the commands are carried out (in other words an "and" formula)? Likewise, is there a similar "or" formula so I don't have to make 4 or more of the same thing?
User avatar
hob_goblin
Forum Regular
Posts: 978
Joined: Sun Apr 28, 2002 9:53 pm
Contact:

Post by hob_goblin »

and

Code: Select all

if(TRUE && TRUE){
}
or

Code: Select all

if(TRUE || FALSE){
}
Bennettman
Forum Contributor
Posts: 130
Joined: Sat Jun 15, 2002 3:58 pm

Post by Bennettman »

So it'd be (for example):


and

Code: Select all

if ($first == 1 && $second == 2) { code; }

or

Code: Select all

if ($first == 1 || $second == 2) { code; }

right?
User avatar
hob_goblin
Forum Regular
Posts: 978
Joined: Sun Apr 28, 2002 9:53 pm
Contact:

Post by hob_goblin »

Bennettman
Forum Contributor
Posts: 130
Joined: Sat Jun 15, 2002 3:58 pm

Post by Bennettman »

Mmkay. Cheerz :) ::drinks Coke::
User avatar
phpPete
Forum Commoner
Posts: 97
Joined: Sun Aug 18, 2002 4:40 pm
Location: New Jersey

Post by phpPete »

one suggestion, make your conditions distinct, by nesting each in its own parens.
So:

Code: Select all

if ($first == 1 && $second == 2) { code; }
becomes

Code: Select all

if ( ($first == 1) && ($second == 2) ) { code; }
In essense, the first way the whole if is one condition, the second way each condition is distinct.
Bennettman
Forum Contributor
Posts: 130
Joined: Sat Jun 15, 2002 3:58 pm

Post by Bennettman »

Makes sense.
User avatar
Takuma
Forum Regular
Posts: 931
Joined: Sun Aug 04, 2002 10:24 am
Location: UK
Contact:

Post by Takuma »

phpPete -> do they make any difference?
User avatar
Johnm
Forum Contributor
Posts: 344
Joined: Mon May 13, 2002 12:05 pm
Location: Michigan, USA
Contact:

Post by Johnm »

Basic mathematics guys, the items in the inner parens will be operated on before the overall equation gets processed. ex:

((1 + 1) - (2 + 2)) = ( (2) - (4) ) = -2

where as:

(1 + 1 - 2 + 2) = 2


See the difference it can make.



Direwolf
User avatar
Takuma
Forum Regular
Posts: 931
Joined: Sun Aug 04, 2002 10:24 am
Location: UK
Contact:

Post by Takuma »

Oh IT's that simple! :lol:
User avatar
nielsene
DevNet Resident
Posts: 1834
Joined: Fri Aug 16, 2002 8:57 am
Location: Watertown, MA

Post by nielsene »

The parenthesis can be required by some languages, C IIRC would change if (a==b && c==d) internally into if (a==(b&&c)==d) due to its operator priority.(That doesn't sound right, but I know that the order was slightly non-intuitive.) So people from a C background will religiously parenthesis comparisions.

If's are a language construct not a function, as such they do not follow normal evaluation rules and all the subexpressions are NOT fully evaluated ahead of time, regardless of the parenthesis because of the promise that the if will short circuit when possible.

If it were math this would be like
(1+1) - (2+2) evaluating to 2 - (2+2) and then realizing that we don't need to finish for some reason.

This is often the (only) reason why something is a language construct versus a built-in function.
User avatar
mikeq
Forum Regular
Posts: 512
Joined: Fri May 03, 2002 3:33 am
Location: Edinburgh, Scotland

Post by mikeq »

You can also use the words 'and' 'or'

if (($a==$c) and ($b==$d)){
...
}

if (($a==$c) or ($b==$d)){
...
}

but there is a difference in the precedence of these operators so use of parenthesis is essential to make sure it does what you want.
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

operator's precedence assures that something like
if ($a == 2 && $b == 4)
is evaluated as expected. == binds stronger than &&, so it is evaluated first => no difference between
if ($a == 2 && $b == 4) and if (($a == 2) && ($b == 4))

therefor

Code: Select all

$a = 3;
if ( ($a == 3) ? 1 : 0 + 2 == 3 && $a|1 != 0 || $a%2 == 0)
...
evaluates to TRUE - but nobody wants to read something like that ;)

on the other hand I don't like

Code: Select all

if ( (((($a == 3) ? 1 : 0) + 2) == 3) && ((($a|1) != 0) || (($a%2) == 0)) )
either :D
User avatar
Takuma
Forum Regular
Posts: 931
Joined: Sun Aug 04, 2002 10:24 am
Location: UK
Contact:

Post by Takuma »

OK I get it thanks volka (you've confused me... :lol: )
User avatar
gotDNS
Forum Contributor
Posts: 217
Joined: Tue May 07, 2002 5:53 pm
Location: West Chester, PA

Post by gotDNS »

COCA-COLA ROCK, Bennettman..u TAKE ur help, and u KEEP drinkin it! YEAH!....remember your priorities, though....

COKE FIRST!

later on, -Brian :P
Post Reply