简体   繁体   English

php“||”赋值逻辑与Javascript相比

[英]php “||” assignment logic compared to Javascript

I was testing Heredoc and || 我正在测试Heredoc和|| functionality 功能

function test()
{
    $obj = [(object)['col1'=>'val1'],
            (object)['col2'=>'val2'],
            (object)['col3'=>'val3']];
    $output = '';
$mystring = <<<EOT
a non empty string
EOT;

    foreach($obj as $prop)
    {
        foreach($prop as $i)
        {
            $output .= <<<EOT
            <div style='background-color:lightblue'> 
            \$head  : {gettype($i)} 
            </div>
EOT;
        }
    }
    $out =  $output || $mystring || 'couldn\'t find something valuable';
    echo $out;
}
test();

I get the output of 我得到了输出

1 1

which represents a Boolean true. 表示布尔值为true。 I had it output the contents of $mystring at one point by putting the logic in brackets eg 我让它通过将逻辑放在括号中来一次输出$ mystring的内容

echo ($output || $mystring);

and it used to output: 它曾用于输出:

a non empty string 非空字符串

It stopped working straight after and I am not sure what change broke it. 它直接停止工作,我不确定是什么改变打破了它。

In javascript: 在javascript中:

  function test() { var obj = [{'col1':'val1'}, {'col2':'val2'}, {'col3':'val3'}]; var output = ''; var mystring ="a non empty string"; for(var prop in obj) { for(var i in prop) { output += "<div style='background-color:lightblue'>" + i + " : " + typeof prop[i] + "</div>"; } } out = output || mystring || 'couldn\\'t find something valuable'; document.write(out); console.log("outputting \\n\\t" + out); } test(); 

It is slightly different from the php in terms of logic but the || 它在逻辑方面与php略有不同,但是|| during assignment works as expected. 在任务期间按预期工作。 I get 我明白了

0 : string 0:字符串

0 : string 0:字符串

0 : string 0:字符串

for the code above. 对于上面的代码。 If comment out the inner for loop like so 如果注释掉内部for循环就像这样

for(var prop in obj)
{
    /*for(var i in prop)
    {
        output += 
        "<div style='background-color:lightblue'>" +
        i + " : " + typeof prop[i] +
        "</div>";
    }*/
}

I get the contents of "mystring" which is 我得到了“mystring”的内容

a non empty string 非空字符串

and if I then change mystring to an empty string like so 如果我然后将mystring更改为空字符串就像这样

mystring = ""; //"a non empty string";

I get 我明白了

couldn't find something valuable 找不到有价值的东西

How does php's "||" php的“||”怎么样 during assignment work ? 在任务工作期间? Can someone explain it? 有人可以解释一下吗?

If you assign a conditional expression in php you always get a boolean (that is, strictly true or false ), it does not work as in javascript where the "truthy" value gets assigned, so in PHP 如果你在php中分配一个条件表达式,你总是得到一个布尔值(即严格为truefalse ),它在javascript中不能正常工作,其中“truthy”值被赋值,所以在PHP中

$a = "";
$b = 42;
$c = ($a || $b); // == true

While in javascript 在javascript中

var a = "";
var b = 42;
var c = (a || b); // == 42

and this is, sadly, by language design. 遗憾的是,这是语言设计。

As @billyonecas reports, it is also in the comments to the docs: http://php.net/manual/en/language.operators.logical.php#77411 正如@billyonecas报道的那样,它也在对文档的评论中: http ://php.net/manual/en/language.operators.logical.php#77411

To obtain a similar behaviour in PHP, we must do something like: 要在PHP中获得类似的行为,我们必须执行以下操作:

$a = "";
$b = 42;
$c = ($a ?: $b); // newer php version
$c = ($a ? $a : $b); // older php version

if you need to concatenate expression, use parethesis: 如果需要连接表达式,请使用parethesis:

$c = ($a ? $a : ($b ? $b : "not found"));

With PHP, you can use ?: 使用PHP,您可以使用?:

$out =  $output ?: $mystring;

Or in older versions of PHP: 或者在旧版本的PHP中:

$out =  $output ? $output : $mystring;

You can also use empty() to get a more explicit code: 您还可以使用empty()来获取更明确的代码:

$out =  empty($output) ? $mystring : $output;

http://php.net/manual/en/function.empty.php http://php.net/manual/en/function.empty.php

Note: these ternary expressions are equivalent to if-else: 注意:这些三元表达式等同于if-else:

if(empty($output))
{
    $out = $mystring;
}
else
{
    $out = $output;
}

With nested ternary, add parentheses, as the default precedence is: 使用嵌套三元组,添加括号,默认优先级为:

$out =  $output ? $output : $mystring ? $mystring : 'default';

This is: 这是:

$out =  ($output ? $output : $mystring) ? $mystring : 'default';

So you must do: 所以你必须这样做:

$out =  $output ? $output : ($mystring ? $mystring : 'default');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM