2015-05-14 56 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