php: converting to boolean

Trabla: php: converting to boolean

Solving:

Convert value to boolean use (bool) or (boolean) casts.

Examples:

$a = 0.0;  // $a  float 0.0
$a = (bool) $a; //$a bool FALSE

OR

$a = -1;  // $a  negative number
$a = (boolean) $a; //$a bool TRUE

Type Juggling - http://php.net/manual/en/language.types.type-juggling.php

Will convert to FALSE examples:

1. integer 0 (zero)  => FALSE
2. float  0.0 (zero) => FALSE
3. empty string and string "0" => FALSE
4. array with zero elements  => FALSE
5. NULL  => FALSE
6. SimpleXML objects created from empty tags => FALSE

EVERY OTHER VALUE (including any resources )=> TRUE

ATTENTION!!!
-1  =>  TRUE
any other non-zero (negative or positive) number => TRUE

Official Docs: http://php.net/manual/en/language.types.boolean.php

No comments:

Post a Comment