简体   繁体   中英

Why does boolval(false) return empty in php?

I'm a little confused how the php function boolval works. Consider the following

<?php echo boolval(true); // prints 1

And contrast with

<?php echo boolval(false); // prints nothing?

Why am I not getting back either true/false? And why is boolval(false) returning nothing?

I ran into this problem while trying to parse $argv for a boolean argument. What's the correct of extracting a bool value from $argv if not with this function?

Following the examples in the documentation, this would be the way to show boolean values:

echo 'false: '.(boolval(false) ? 'true' : 'false')."\n";

See: http://php.net/manual/en/function.boolval.php

The manual also says:

A boolean TRUE value is converted to the string "1". Boolean FALSE is converted to "" (the empty string). This allows conversion back and forth between boolean and string values.

See: http://php.net/manual/en/language.types.string.php

From the php manual

A boolean TRUE value is converted to the string "1". Boolean FALSE is converted to "" (the empty string). This allows conversion back and forth between boolean and string values.

use

echo $boolres ? 'true' : 'false';

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