checking multiple if statements

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
a94060
Forum Regular
Posts: 543
Joined: Fri Feb 10, 2006 4:53 pm

checking multiple if statements

Post 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?
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post 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') { }
User avatar
a94060
Forum Regular
Posts: 543
Joined: Fri Feb 10, 2006 4:53 pm

Post by a94060 »

thank you, so the && can be used to only validate the rest of the if statment if the 2 statments are true?
SKDevelopment
Forum Newbie
Posts: 13
Joined: Thu Jan 26, 2006 10:42 am

Post 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
User avatar
newmember
Forum Contributor
Posts: 252
Joined: Fri Apr 02, 2004 12:36 pm

Post 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
User avatar
a94060
Forum Regular
Posts: 543
Joined: Fri Feb 10, 2006 4:53 pm

Post by a94060 »

ok thanks,thats just what i wanted!
User avatar
shiznatix
DevNet Master
Posts: 2745
Joined: Tue Dec 28, 2004 5:57 pm
Location: Tallinn, Estonia
Contact:

Post 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
}
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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.
User avatar
a94060
Forum Regular
Posts: 543
Joined: Fri Feb 10, 2006 4:53 pm

Post 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.
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post 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.
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
a94060
Forum Regular
Posts: 543
Joined: Fri Feb 10, 2006 4:53 pm

Post 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?
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post 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
}
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
a94060
Forum Regular
Posts: 543
Joined: Fri Feb 10, 2006 4:53 pm

Post by a94060 »

ok thanks
Post Reply