note 21750 added to language.expressions

From: Date: Sat, 25 May 2002 07:29:35 +0000
Subject: note 21750 added to language.expressions
Groups: php.notes 
Request: Send a blank email to php-notes+get-31021@lists.php.net to get a copy of this message
A note about the short-circuit behaviour of the boolean operators. 1. if (func1() || func2()) Now, if func1() returns true, func2() isn't run, since the expression will be true anyway. 2. if (func1() && func2()) Now, if func1() returns false, func2() isn't run, since the expression will be false anyway. The reason for this behaviour comes probably from the programming language C, on which PHP seems to be based on. There the short-circuiting can be a very useful tool. For example: int * myarray = a_func_to_set_myarray(); // init the array if (myarray != NULL && myarray[0] != 4321) // check myarray[0] = 1234; Now, the pointer myarray is checked for being not null, then the contents of the array is validated. This is important, because if you try to access an array whose address is invalid, the program will crash and die a horrible death. But thanks to the short circuiting, if myarray == NULL then myarray[0] won't be accessed, and the program will work fine. -- http://www.php.net/manual/en/language.expressions.php http://master.php.net/manage/user-notes.php?action=edit+21750 http://master.php.net/manage/user-notes.php?action=delete+21750 http://master.php.net/manage/user-notes.php?action=reject+21750

« previous php.notes (#31021) next »