note 118537 deleted from function.array-search by cmb

From: Date: Sun, 01 Sep 2019 07:58:28 +0000
Subject: note 118537 deleted from function.array-search by cmb
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-212079@lists.php.net to get a copy of this message
Note Submitter: hyder7nh at gmail dot com ---- When using array_search in if statements with array elements that use integers for keys, if the key [0] matches, the statement would always return false. This is because array_search returns the key of the value that matches with the first argument. If statements check if the return value is true or false(boolean), 0 is boolean for false. This problem shouldn't occur with key names that are strings. <?php //$f would be 0, because key [0] matches the first argument $f = array_search('photo', array('photo', 'audio')); //$f statement would fall to else condition because $f is 0 and 0 is false if($f==true) { echo 'found<br/>'; } else { echo 'not found<br/>'; } if(array_search('photo', array('photo'))) { echo 'FOUND'; } else { echo 'NOT FOUND'; } Use the === operator to get around this problem.

« previous php.notes (#212079) next »