note 61110 added to function.array-search

From: Date: Wed, 25 Jan 2006 21:32:51 +0000
Subject: note 61110 added to function.array-search
Groups: php.notes 
Request: Send a blank email to php-notes+get-102450@lists.php.net to get a copy of this message
This has been noted but took me a while to figure out: When the array that you are searching starts with position '0' array_search returns '0' for a match - evaluating as FALSE: $usernameArray[0]='moe'; $usernameArray[1]='larry'; $usernameArray[2]='curly'; $username='moe'; // broken if ($userNameArrayPosition=array_search($username, $usernameArray)) { // even though the expression is true, will eval as false echo 'userNameArrayPosition -'.$userNameArrayPosition; }else{ echo 'evaluates false - userNameArrayPosition = -'.$userNameArrayPosition; } // will echo 'evaluates false - userNameArrayPosition = - 0' // works if (is_numeric($userNameArrayPosition=array_search($username, $usernameArray)) { // will be true for all numeric values, but not bool. false echo 'userNameArrayPosition -'.$userNameArrayPosition; }else{ echo 'evaluates false - userNameArrayPosition = -'.$userNameArrayPosition; } // will echo 'userNameArrayPosition - 0' $username='homer'; if (is_numeric($userNameArrayPosition=array_search($username, $usernameArray)) { // will be true for all numeric values, but not bool. false echo 'userNameArrayPosition -'.$userNameArrayPosition; }else{ echo 'evaluates false - userNameArrayPosition = -'.$userNameArrayPosition; } // will echo 'evaluates false - userNameArrayPosition = - FALSE' ---- Server IP: 216.194.113.175 Probable Submitter: 216.176.226.161 ---- Manual Page -- http://www.php.net/manual/en/function.array-search.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+61110 Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+61110&report=yes&reason=added+to+the+manual Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+61110&report=yes&reason=bad+code Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+61110&report=yes&reason=spam Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+61110&report=yes&reason=useless Delete: non-english -- http://master.php.net/manage/user-notes.php?action=delete+61110&report=yes&reason=non-english Delete: already in docs -- http://master.php.net/manage/user-notes.php?action=delete+61110&report=yes&reason=already+in+docs Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+61110&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+61110&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#102450) next »