Conversion: 1. Use the double logical NOT operator with the syntax "!!Number to be converted"; 2. Add the target type "(bool)" enclosed in parentheses before the numerical variable to be converted. Or "(boolean)", for example "(bool) value"; 3. Use the boolval() function, the syntax is "boolval (value)".
The operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer
php converts numerical values For Boolean type
1, use the double logical NOT operator "!!
"
to perform a Boolean value Negation operation. For example: !true = false, !false = true, !10 = false.
<?php var_dump(!!2); //返回true var_dump(!!0); //返回false var_dump(!!1); //返回true ?>
2. Add the target type "(bool)" or "(boolean)" enclosed in parentheses before the conversion variable
(bool), (boolean): Convert to Boolean type
<?php var_dump((bool)2); //返回true var_dump((bool)0); //返回false var_dump((bool)1); //返回true var_dump((bool)100); //返回true ?>
3. Use boolval() function
boolval(): used to obtain the Boolean value of the variable;
<?php echo '0: '.(boolval(0) ? 'true' : 'false')."<br>"; echo '42: '.(boolval(42) ? 'true' : 'false')."<br>"; echo '0.0: '.(boolval(0.0) ? 'true' : 'false')."<br>"; echo '4.2: '.(boolval(4.2) ? 'true' : 'false')."<br>"; echo '"0": '.(boolval("0") ? 'true' : 'false')."<br>"; echo '"1": '.(boolval("1") ? 'true' : 'false')."<br>"; ?>
Output result:
0: false 42: true 0.0: false 4.2: true "0": false "1": true
Recommended learning: "PHP Video Tutorial》
The above is the detailed content of How to convert numeric value to boolean type in php. For more information, please follow other related articles on the PHP Chinese website!