简体   繁体   中英

php how to differentiate between 0 or 1 and boolean

In some cases the key which is returned by array_search() is 0 or 1. When I use this return value in an if-statement, php interprets it as boolean.. but I want it to be interpreted as a string. How can i do this?

example:

$array=array('abc','a','b');

$returnvalue=array_search('abc',$array); // will be 0

if($returnvalue!=false){
statement // right now, this will not be executed
}

thanks a lot for your help!

Use type safe comparisons:

if ($returnvalue !== false)

The same is also supported for ==, with ===:

if ($returnvalue === false)
{
    // not found
}

== compare for value

=== compare for value and data type

See:

$array = array('abc','a','b');
$returnvalue = array_search('abc', $array); // will be 0

if($returnvalue == false){
    // Executed
    var_dump($returnvalue);
}

if($returnvalue === false){
    // Not executed
    var_dump($returnvalue);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM