note 41528 deleted from function.in-array by aidan
| From: | aidan@php.net | Date: | Wed, 18 Aug 2004 06:22:52 +0000 |
| Subject: | note 41528 deleted from function.in-array by aidan | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-74852@lists.php.net to get a copy of this message | ||
Note Submitter: Noah B
----
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.