PHP Boolean

PHP教程 - PHP Boolean

布尔值保持真或假。 在幕后,布尔是整数。

假值

此外,PHP将以下值视为 false :

  • The literal value false
  • The integer zero (0)
  • The float zero ( 0.0 )
  • An empty string ( " " )
  • The string zero ( "0" )
  • An array with zero elements
  • The special type null (including any unset variables)
  • A SimpleXML object that is created from an empty XML tag

所有其他值在布尔上下文中被视为true。

我们可以为布尔类型变量赋值true或false值。


<?PHP
    $bool = true;
    print "Bool is set to $bool\n";
    $bool = false;
    print "Bool is set to $bool\n";
?>

上面的代码生成以下结果。



实施例2

在第二个if语句中,我们比较整数值到布尔值。


<?PHP/* w ww  .  j a  va 2s  . co  m*/
$a=100;
if($a==100) {
    echo "the variable equals 1!\n";
}

if($a==true) {
   echo "the variable is true!";
}
?>

上面的代码生成以下结果。



0.0649s