note 129682 added to control-structures.if

From: Date: Wed, 31 Jul 2024 09:46:42 +0000
Subject: note 129682 added to control-structures.if
Groups: php.notes 
Request: Send a blank email to php-notes+get-221454@lists.php.net to get a copy of this message
Left-to-right evaluation of && operators has one useful feature: evaluation stops after first "false" operand is encountered. This feature can be useful for creating following construction: $someVar==123 is not evaluated, so there will be no warnings such as "Undefined variable $someVar": <?php // $someVar=123; - commented line if ((!empty($someVar))&&($someVar==123)) { echo $someVar; } ?> Function someFunc($someVar) will not be called: <?php // $someVar=123; - commented line if ((!empty($someVar))&&(someFunc($someVar))) { echo $someVar; } ?> This will give "Warning: Undefined variable $someVar" error. Order matters: <?php // $someVar=123; if ((someFunc($someVar))&&(!empty($someVar))) { echo $someVar; } ?> ---- Server IP: 45.112.84.4 Probable Submitter: 80.90.5.137 (proxied: 111.27.10.63) ---- Manual Page -- https://php.net/manual/en/control-structures.if.php Edit -- https://main.php.net/note/edit/129682 Del: integrated -- https://main.php.net/note/delete/129682/integrated Del: useless -- https://main.php.net/note/delete/129682/useless Del: bad code -- https://main.php.net/note/delete/129682/bad+code Del: spam -- https://main.php.net/note/delete/129682/spam Del: non-english -- https://main.php.net/note/delete/129682/non-english Del: in docs -- https://main.php.net/note/delete/129682/in+docs Del: other reasons-- https://main.php.net/note/delete/129682 Reject -- https://main.php.net/note/reject/129682 Search -- https://main.php.net/manage/user-notes.php

« previous php.notes (#221454) next »