Jump to content

acceptable practice SOLVED with thanks in final post


niche

Recommended Posts

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?

Edited by niche
Link to comment
Share on other sites

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.

  • Like 1
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...