note 118537 added to function.array-search

From: Date: Wed, 23 Dec 2015 08:34:25 +0000
Subject: note 118537 added to function.array-search
Groups: php.notes 
Request: Send a blank email to php-notes+get-204524@lists.php.net to get a copy of this message
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. ---- Server IP: 72.52.91.14 Probable Submitter: 59.92.229.216 ---- Manual Page -- http://php.net/manual/en/function.array-search.php Edit -- https://master.php.net/note/edit/118537 Del: integrated -- https://master.php.net/note/delete/118537/integrated Del: useless -- https://master.php.net/note/delete/118537/useless Del: bad code -- https://master.php.net/note/delete/118537/bad+code Del: spam -- https://master.php.net/note/delete/118537/spam Del: non-english -- https://master.php.net/note/delete/118537/non-english Del: in docs -- https://master.php.net/note/delete/118537/in+docs Del: other reasons-- https://master.php.net/note/delete/118537 Reject -- https://master.php.net/note/reject/118537 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#204524) next »