PHP中的布尔赋值运算符 [英] Boolean assignment operators in PHP

查看:110
本文介绍了PHP中的布尔赋值运算符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现自己经常做这种事情:

I find myself doing this kind of thing somewhat often:

$foo = true;
$foo = $foo && false; // bool(false)

通过按位运算符,可以使用&=|=速记:

With bitwise operators, you can use the &= and |= shorthand:

$foo = 1;
$foo &= 0; // int(0)

鉴于10的按位运算在功能上等同于truefalse的布尔运算,我们可以依靠类型转换并执行以下操作:

Given that bitwise operations on 1 and 0 are functionally equivalent to boolean operations on true and false, we can rely on type-casting and do something like this:

$foo = true;
$foo &= false; // int(0)
$foo = (bool)$foo; // bool(false)

...但这很丑陋,并且无法达到使用速记分配语法的目的,因为我们必须使用另一条语句将类型恢复为布尔值.

...but that's pretty ugly and defeats the purpose of using a shorthand assignment syntax, since we have to use another statement to get the type back to boolean.

我真正想做的是这样的:

What I'd really like to do is something like this:

$foo = true;
$foo &&= false; // bool(false)

...但是&&=||=显然不是有效的运算符.因此,我的问题是-是否还有其他一些含糊的语法,或者可能是不起眼的核心功能可以作为替代品?对于只有$foo的变量,使用$foo = $foo && false语法并不是什么大问题,但是具有多个维度的数组元素和/或对象方法调用可以使语法相当冗长.

...but &&= and ||= are not valid operators, obviously. So, my question is - is there some other sugary syntax or maybe an obscure core function that might serve as a stand-in? With variables as short as $foo, it's not a big deal to just use $foo = $foo && false syntax, but array elements with multiple dimensions, and/or object method calls can make the syntax quite lengthy.

推荐答案

在某种程度上,您已经回答了自己的问题:

In a way you have answered your own question:

在1和0上的按位运算在功能上等效于在true和false上的布尔运算

bitwise operations on 1 and 0 are functionally equivalent to boolean operations on true and false

请记住,PHP是一种弱类型的语言,因此不需要进行强制布尔值的强制类型转换,因为10等同于truefalse(严格相等除外,请参见下文).

Bearing in mind that PHP is a weakly typed language, so it is not necessary to typecast to and from strict boolean values as 1 and 0 are equivalent to true and false (except strict equality, see below).

使用示例考虑以下代码:

Consider the following code, using your examples:

$foo = true;
$foo &= false;

if (!$foo) {
  echo 'Bitwise works!';
}

$bar = true;
$bar = $bar && false;

if (!$bar) {
  echo 'Boolean works!';
}

// Output: Bitwise works!Boolean works!

给出PHP的隐式类型变戏法虚假值,但

Given PHP's implicit type juggling, falsy values, and with the exception of strict equaltiy, I'm hard pressed to see where such shorthand operations of &&= and ||= would not yield the same result as &= and |=. Especially when evaluating boolean values. It's likely why such shorthands don't exist in PHP.

一些快速基准测试证明它们确实是等效的,除了 truthy 数组/对象:

Some quick benchmarks prove these are indeed equivalent, except for truthy arrays/objects:

<?php
$values = array(false, 0, 0.0, "", "0", array(), 12, "string", array(1));

foreach ($values as $value) {
    $bit_test = true;
    $bit_test &= $value;

    $bool_test = true;
    $bool_test = $bool_test && false;
    if ($bit_test != $bool_test) {
        echo 'Difference for: ';
        var_dump($value);
    }
}

// Output:
// Difference for: array(1) {
//  [0]=>
//  int(1)
// }

这篇关于PHP中的布尔赋值运算符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆