2015-05-14 31 views
1

我正在测试Heredoc和||功能php“||”赋值逻辑相比于Javascript

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(); 

我得到的

输出代表一个布尔真。 我有它输出的$通过将逻辑括号在一个点myString的内容,例如

echo ($output || $mystring); 

,并用于输出:

一个非空字符串

它停止工作后,我不知道什么样的变化打破了它。

在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();

距离PHP的逻辑但方面略有不同||在分配工作中按预期工作。我得到

0:字符串

0:字符串

0:字符串

上面的代码。如果注释掉内的循环,像这样

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

我得到“MyString的”,这是

一个非空字符串的内容

,如果我再改的MyString到像这样的空字符串

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

我得到

找不到有价值的东西

如何PHP的 “||”在分配工作中? 有人可以解释它吗?

+0

请修复您的代码格式。函数test(){'和'} test()'是未格式化的。 – Gogol

+0

@ noc2spamツ谢谢。在我阅读您的评论之前修复它.XD – Sarfaraaz

回答

2

如果分配条件表达式在PHP中,你总能获得一个布尔值(即,严格truefalse),它不工作在那里的“truthy”值被分配JavaScript,因此在PHP

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

虽然在JavaScript

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

,这是可悲的是,通过语言的设计。

由于@billyonecas报告,它也是在评论文档:http://php.net/manual/en/language.operators.logical.php#77411

为了获得在PHP中类似的行为,我们必须这样做:如果你需要连接

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

表达,使用parethesis:

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

http://php.net/manual/en/language.operators.logical。php#77411和http://en.wikipedia.org/wiki/Short-circuit_evaluation – billyonecan

+0

好的。但我确实通过在其中加上括号使它暂时工作。为什么会这样呢? – Sarfaraaz

+2

我真的不知道,就我所知,它只是**不能**而已。 –

1

在PHP中,你可以使用?:

$out = $output ?: $mystring; 

或者在旧版本的PHP:

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

您还可以使用empty()获得更明确的代码:

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

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

注:这些三元表达式相当于if-else语句:

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

对于嵌套三元,加个括号,默认优先级为:

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

这就是:

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

所以你必须这样做:

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

你说empty()获得更明确的代码。就像你会从var_dump()或json_encode()那里得到的输出一样? < - (更具描述性,更易于阅读) – Sarfaraaz

+0

我试过$ out = $ output? $输出:$ mystring? $ mystring:'找不到有价值的东西';就在我读你的帖子之前。我喜欢你启发我的较新的PHP版本。我得到了$ mystring的内容(非空字符串),而不是$ output的内容。不知道为什么 – Sarfaraaz

+1

只需加上括号:'$ out = $ output? $ output:($ mystring?$ mystring:'could not find something valuable');'我不明白你的第一条评论。 – KyleK