PHP Data type boolean

Boolean types fall into two categories:

  1. TRUE: Indicates true, valid, real
  2. FALSE: Indicates that the fact is not true, invalid, fake

For example, compare two numbers (== is a comparison operator, see PHP Operators):

$num1 = 2;
$num2 = 5;
if ($num1 == $num2) {
    echo '$num1 is equal to $num2';
} Else {
    echo '$num1 is not equal to $num2';

Run the example, the browser output:

$num1 is not equal to $num2

For the $num1 == $num2 comparison results FALSE is a logical result, and can not directly call the echo output FALSE, but should be used to obtain process control. See PHP Flow Control for process control.

Convert to Boolean

In many cases, we need to determine the value of the Boolean, in most cases do not need to use cast, because when the operator, function or process control requires a boolean parameter, the value will be automatically converted.

When other types are converted to boolean, the following values ​​are considered FALSE:

  • Integer value 0 (zero)
  • A float value of 0.0 (zero)
  • Blank string and string "0"
  • Array with no member variables
  • Objects without cells (PHP 4 only)
  • Special type NULL (including variables not yet set)

All other values ​​except those listed above are considered TRUE (including any resources).

Copyright 2014-2017 by All Rights Reserved.