niche Posted September 6, 2012 Share Posted September 6, 2012 Is this acceptable practice (letting the 1st condition shield the second)? <?phpif (isset($var) and $var == "Y") {echo "here";}?> Obviously, reversing the conditions produces an error, but this way doesn't and it's convenient. Issues, or do the ends justify the the means in this case? Link to comment Share on other sites More sharing options...
Ingolme Posted September 6, 2012 Share Posted September 6, 2012 Yes, it's fine to do that.I usually do something like this instead, though: $var = isset($x) ? $x : "default value"; Link to comment Share on other sites More sharing options...
niche Posted September 6, 2012 Author Share Posted September 6, 2012 Thanks, as always, Ingolme. Link to comment Share on other sites More sharing options...
justsomeguy Posted September 6, 2012 Share Posted September 6, 2012 That's called short-circuiting, which both PHP and Javascript support. If there are multiple conditions separated by AND or OR, and one of them evaluates to a value that will "solve" the entire statement (false for AND, true for OR), then evaluation stops after that condition. So if you have an AND condition and the first expression evaluates to false, it will not evaluate the other expressions. That's why this works also: $result = mysql_query($sql) or exit(mysql_error()); Since that is an or operator, if the first expression evaluates to true it does not evaluate (execute) the next one. The error only shows if the first expression evaluates to false. Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.