By Martin / PHP / Monday August 25th, 2008 / No comments
Just an interesting thing I discovered - if you place a series of functions in a conditional with AND comparison, they are executed sequentially and as soon as one returns false the conditional is exited, without executing the others.
For example, consider this if statement:
if (function1() && function2()) { }
In this statement, first function1() is run. If it returns true, the interpreter goes on to execute function2(), but if it returns false then function2() is not called! This means that in the following case nothing will be outputted.
function function1() {
return false;
}
function function2() {
echo "Function2 was run!";
return true;
}
if (function1() && function2()) echo "Both true";
∎
I hope you enjoyed reading this article. If you wish, you may view some of the other recent or popular things I have written, or subscribe to receive RSS updates. You can also add a comment, or share this article on Twitter or Facebook, below.
Website copyright © Aspektas 2009 - 2010 Valid XHTML 1.0 Strict and CSS 2.1