note 41528 added to function.in-array

From: Date: Tue, 13 Apr 2004 18:12:14 +0000
Subject: note 41528 added to function.in-array
Groups: php.notes 
Request: Send a blank email to php-notes+get-68083@lists.php.net to get a copy of this message
An interesting gotcha in in_array(): 0 or 1 in the array will elvaluate as true and false, so this code doesn't behave as one might expect: $numerals=range(0,9); $testval='foo'; //this can be anything if (in_array($testval,$numerals)){ echo 'this will always print'; } No matter what $testval is set to, it will be either true or false, so it matches either 1 or 0, and in_array() will return true. If you use range('0','9') instead (note quotes), this works as expected. ---- Manual Page -- http://www.php.net/manual/en/function.in-array.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+41528 Delete -- http://master.php.net/manage/user-notes.php?action=delete+41528&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+41528&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#68083) next »