Page 1 of 2
if ___ and ___ { }
Posted: Mon Aug 26, 2002 9:57 am
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?
Posted: Mon Aug 26, 2002 9:58 am
by hob_goblin
Posted: Mon Aug 26, 2002 10:01 am
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?
Posted: Mon Aug 26, 2002 10:02 am
by hob_goblin
Posted: Mon Aug 26, 2002 10:10 am
by Bennettman
Mmkay. Cheerz

::drinks Coke::
Posted: Mon Aug 26, 2002 10:50 am
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.
Posted: Mon Aug 26, 2002 11:04 am
by Bennettman
Makes sense.
Posted: Mon Aug 26, 2002 2:32 pm
by Takuma
phpPete -> do they make any difference?
Posted: Mon Aug 26, 2002 2:43 pm
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
Posted: Mon Aug 26, 2002 2:46 pm
by Takuma
Oh IT's that simple!

Posted: Mon Aug 26, 2002 2:54 pm
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.
Posted: Tue Aug 27, 2002 4:31 am
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.
Posted: Tue Aug 27, 2002 5:47 am
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

Posted: Tue Aug 27, 2002 3:23 pm
by Takuma
OK I get it thanks volka (you've confused me...

)
Posted: Wed Aug 28, 2002 9:22 am
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
