[go: up one dir, main page]

PHP 8.1.30 Released!

Voting

: three plus five?
(Example: nine)

The Note You're Voting On

cue at openxbox dot com
21 years ago
If you are using the result of array_search in a condition statement, make sure you use the === operator instead of == to test whether or not it found a match. Otherwise, searching through an array with numeric indicies will result in index 0 always getting evaluated as false/null. This nuance cost me a lot of time and sanity, so I hope this helps someone. In case you don't know what I'm talking about, here's an example:

<?php
$code
= array("a", "b", "a", "c", "a", "b", "b"); // infamous abacabb mortal kombat code :-P

// this is WRONG
while (($key = array_search("a", $code)) != NULL)
{
// infinite loop, regardless of the unset
unset($code[$key]);
}

// this is _RIGHT_
while (($key = array_search("a", $code)) !== NULL)
{
// loop will terminate
unset($code[$key]);
}
?>

<< Back to user notes page

To Top