note 100538 added to control-structures.if
| From: | zimmermanj2 at xavier dot edu | Date: | Fri, 22 Oct 2010 02:12:58 +0000 |
| Subject: | note 100538 added to control-structures.if | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-173361@lists.php.net to get a copy of this message | ||
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.
----
Server IP: 69.147.83.197
Probable Submitter: 24.209.244.220
----
Manual Page -- http://www.php.net/manual/en/control-structures.if.php
Edit -- https://master.php.net/note/edit/100538
Del: integrated -- https://master.php.net/note/delete/100538/integrated
Del: useless -- https://master.php.net/note/delete/100538/useless
Del: bad code -- https://master.php.net/note/delete/100538/bad+code
Del: spam -- https://master.php.net/note/delete/100538/spam
Del: non-english -- https://master.php.net/note/delete/100538/non-english
Del: in docs -- https://master.php.net/note/delete/100538/in+docs
Del: other reasons-- https://master.php.net/note/delete/100538
Reject -- https://master.php.net/note/reject/100538
Search -- https://master.php.net/manage/user-notes.php