note 61108 deleted from function.array-search by danbrown

From: Date: Wed, 29 Apr 2009 13:03:27 +0000
Subject: note 61108 deleted from function.array-search by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-153988@lists.php.net to get a copy of this message
Note Submitter: chrisAdams at 407x dot com ---- 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)==TRUE) { // 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)==TRUE) { // 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'

« previous php.notes (#153988) next »