codecamp

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!";
}
?>

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



PHP整数
PHP数字转换
温馨提示
下载编程狮App,免费阅读超1000+编程语言教程
取消
确定
目录

Operator

Introduction

关闭

MIP.setData({ 'pageTheme' : getCookie('pageTheme') || {'day':true, 'night':false}, 'pageFontSize' : getCookie('pageFontSize') || 20 }); MIP.watch('pageTheme', function(newValue){ setCookie('pageTheme', JSON.stringify(newValue)) }); MIP.watch('pageFontSize', function(newValue){ setCookie('pageFontSize', newValue) }); function setCookie(name, value){ var days = 1; var exp = new Date(); exp.setTime(exp.getTime() + days*24*60*60*1000); document.cookie = name + '=' + value + ';expires=' + exp.toUTCString(); } function getCookie(name){ var reg = new RegExp('(^| )' + name + '=([^;]*)(;|$)'); return document.cookie.match(reg) ? JSON.parse(document.cookie.match(reg)[2]) : null; }