note 90386 added to language.operators.logical

From: Date: Sun, 19 Apr 2009 15:32:43 +0000
Subject: note 90386 added to language.operators.logical
Groups: php.notes 
Request: Send a blank email to php-notes+get-153496@lists.php.net to get a copy of this message
Evaluation of logical expressions is stopped as soon as the result is known. If you don't want this, you can replace the and-operator by min() and the or-operator by max(). <?php function a($x) { echo 'Expression '; return $x; } function b($x) { echo 'is '; return $x; } function c($x) { echo $x ? 'true.' : 'false.' ;} c( a( false ) and b( true ) ); // Output: Expression false. c( min( a( false ), b( true ) ) ); // Output: Expression is false. c( a( true ) or b( true ) ); // Output: Expression true. c( max( a( true ), b( true ) ) ); // Output: Expression is true. ?> This way, values aren't automaticaly converted to boolean like it would be done when using and or or. Therefore, if you aren't sure the values are already boolean, you have to convert them 'by hand': <?php c( min( (bool) a( false ), (bool) b( true ) ) ); ?> ---- Server IP: 212.124.37.9 Probable Submitter: 139.18.184.98 ---- Manual Page -- http://www.php.net/manual/en/language.operators.logical.php Edit -- https://master.php.net/note/edit/90386 Del: integrated -- https://master.php.net/note/delete/90386/integrated Del: useless -- https://master.php.net/note/delete/90386/useless Del: bad code -- https://master.php.net/note/delete/90386/bad+code Del: spam -- https://master.php.net/note/delete/90386/spam Del: non-english -- https://master.php.net/note/delete/90386/non-english Del: in docs -- https://master.php.net/note/delete/90386/in+docs Del: other reasons-- https://master.php.net/note/delete/90386 Reject -- https://master.php.net/note/reject/90386 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#153496) next »