加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

php – 我应该作为布尔值或假布尔值进行转换吗?

发布时间:2020-12-13 17:20:17 所属栏目:PHP教程 来源:网络整理
导读:如果我得到一个整数变量(从0开始)我可以做一些事情来确保数字不是0(零): 选项1: if($number 0){ // number is not zero} 选项2: if($number){ // number is not zero} 选项3: if((bool) $number){ // number is not zero} 选项4: if(!!$number){ // num
如果我得到一个整数变量(从0开始)我可以做一些事情来确保数字不是0(零):

>选项1:

if($number > 0){
    // number is not zero
}

>选项2:

if($number){
    // number is not zero
}

>选项3:

if((bool) $number){
    // number is not zero
}

>选项4:

if(!!$number){
    // number is not zero
}

>等等……

上面哪一个被认为是最好的?
还是有更好的选择?

解决方法

使用相同的 comparison operaton,它不做任何类型的杂耍(并且更快).

if ($number !== 0) {
 //         ^^^
 // Number is not identical to 0
}

注意:这假设变量实际上是一个“整数变量”,而不是恰好包含数字的字符串.

if (false == ($number === 0)) {
 //                   ^^^
 // It is false that Number is identical to 0
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读