note 100538 deleted from control-structures.if by danbrown

From: Date: Fri, 22 Oct 2010 02:16:36 +0000
Subject: note 100538 deleted from control-structures.if by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-173362@lists.php.net to get a copy of this message
Note Submitter: zimmermanj2 at xavier dot edu ---- I've seen some people talk about this, and I wanted to expand upon it. I'm in a Programming Languages course at my university, and this talk of how boolean expressions are handled in some languages came up. For example: if (A && B) { // do something } If A is false, then there is no way that A && B can be true, so the second test is not processed. The technical term (at least from what I have learned) is called "short circuiting". The same can go for ORs: if (C || D) { // do something else } If C is true, then C || D must be true, so D is not even checked (why do the extra work). This idea of "short circuiting" can be expanded to even more complex boolean expressions in PHP, and probably most of the more popular programming languages. This being said, it is not a good idea then to use things such as functions inside boolean expressions if those functions have side effects, as it is possible they may never run. If you must use a function that behaves in this manner, you should probably use variables to save the result of the function, and then use these variables in the boolean expression you want to use.

« previous php.notes (#173362) next »