Page 1 of 1

checking multiple if statements

Posted: Fri Feb 24, 2006 4:40 pm
by a94060
i would like do do multiple id checking

Code: Select all

if(condition1,condition2,condition3)  {//i do my action is here
}
question is ,i dont know wat to use at "condition1,condition2,condition3" to divide the 3 things.i know that it is not the comma (,). can someone tell me what i would use there?

Posted: Fri Feb 24, 2006 4:47 pm
by John Cartwright
:arrow: http://ca.php.net/manual/en/language.op ... ogical.php

one example would be

Code: Select all

if ($var == 'foo' && $var2 == 'bar') { }

Posted: Fri Feb 24, 2006 4:54 pm
by a94060
thank you, so the && can be used to only validate the rest of the if statment if the 2 statments are true?

Posted: Fri Feb 24, 2006 5:05 pm
by SKDevelopment
Yes. The result is true only if both statements are true.

But if the first statement is false, the second is not checked at all by the PHP parser. Because the result will be false anyway.

--
Best Regards,
Sergey Korolev
www.SKDevelopment.com

Posted: Fri Feb 24, 2006 5:06 pm
by newmember
so the && can be used to only validate the rest of the if statment if the 2 statments are true?
conditions are checked from left to right until one of them false or all of them true

Posted: Fri Feb 24, 2006 7:58 pm
by a94060
ok thanks,thats just what i wanted!

Posted: Fri Feb 24, 2006 8:10 pm
by shiznatix
don't forget the || statement which means OR. heres a simple table

&& = and
|| = or

and yes, you can write

Code: Select all

if ($somthing == 'asdfg' OR $other == 'qwerty')
{
    //valid syntax
}

Posted: Fri Feb 24, 2006 8:15 pm
by feyd
Although semantically the same, it is important to note that 'or' and '||' have different levels of precedence. '||' has higher priority than 'or' by a few notches.

Posted: Sat Feb 25, 2006 7:48 am
by a94060
ok thanks,that would eliminate a few questions for me....

btw id seen that chart before but i thought it was useless to me...i guess if statments operators and thnigs will be important to me.

Posted: Sat Feb 25, 2006 7:54 am
by s.dot
Personally, learning the difference between when to use || or && was difficult for me.. I don't know if it was this way for other people.

But when I learned that everything between if() will evaluate to TRUE or FALSE it was much easier for me to grasp.

Posted: Sat Feb 25, 2006 7:58 am
by a94060
is it possible to use a TRUE or false from 1 statment in another one?


would it be like:

Code: Select all

if($x==Z){//could i put another variable here to make it say true?
$value=TRUE
}

if($value==TRUE){
echo 'This  is working!!!';
}
would that work?

Posted: Sat Feb 25, 2006 11:49 am
by s.dot

Code: Select all

if($x == $z){
   $value = TRUE;
}

if($value == TRUE){
   // do this
}
would be functionally identical to

Code: Select all

if($x == $z){
   // do this
}

Posted: Sat Feb 25, 2006 12:02 pm
by a94060
ok thanks