|
Posted by Vince Morgan on 01/09/08 01:47
"JP" <jpsebasti@gmail.com> wrote in message
news:d39735da-1162-43b5-8748-c7382d675387@q39g2000hsf.googlegroups.com...
> Thanks for the reply:
>
> I had read that box and now have re-read it and am still confused.
> The warning states:
>
> "Warning
>
> This function may return Boolean FALSE, but may also return a non-
> Boolean value which evaluates to FALSE, such as 0 or "". Please read
> the section on Booleans for more information. Use the === operator for
> testing the return value of this function."
>
> This warning says to me that I cannot use something like
> if (array_search($key,$keys))
> {...}
>
> because match on the array index [0] which would return 0 is similar
> to returning FALSE.
>
> Upon further consideration however, I decided to do something like
> this (which works)
>
> $retval = array_search($key, $keys);
> if ($retval)
> {
> echo "Match found\n";
> }
> else
> {
> if ($retval === 0)
> {
> echo "Match found\n";
> }
> else
> {
> echo "Match not found\n";
> }
> }
>
>
> So it appears that if a FALSE is returned by the function but the 0
> return value really means it matched key [0] in the haystack, you
> should check for this with the === operator.
>
Yes.
if($retval === false)
{
echo "Not found";
}else
{
echo "Yep, got it";
}
if($retval !== false)
{
echo "Yep, got it";
}else
{
echo "Not found";
}
Is more compact.
Vince
[Back to original message]
|