In PHP, when other types are converted to the Bool type, the following error occurs: if (true = 2) in PHP, Will true or false be returned?
The result is true. in this case, I will test other php Data types.
Conclusion:
When converted to the bool type, the following data types are converted to false:
1. integer 0
2. empty string
3. empty array
4. NULL
Please add...
Test code:
Function p ($ title, $ mybool ){
Echo"".$title;
echo var_dump($mybool)."
";
}
Class foo {
Function do_foo (){
Echo "Hello! ";
}
}
Echo"Convert other types in PHP to Bool type
";
// Zero
$ N0 = boolval (0 );
P ("zero:", $ n0 );
// Positive integer
$ N = boolval (2 );
P ("positive integer:", $ n );
// Negative integer
$ Nx = boolval (-2 );
P ("negative integer:", $ nx );
// Character space
$ Ss = boolval ("");
P ("Space:", $ ss );
// Empty string
$ Sn = boolval ("");
P ("null string:", $ sn );
// String
$ S = boolval ("chinacion ");
P ("string:", $ s );
// Empty array
$ An = boolval (array ());
P ("empty array:", $ );
// Array
$ A = boolval (array (0 => 1 ));
P ("array:", $ );
// Null type
$ Nu = boolval (NULL );
P ("NULL:", $ nu );
// Object
$ Bar = new foo;
$ Bar;
$ Obj = boolval ($ bar );
P ("Object:", $ obj );