[英]Which is the most efficient way to work around the first one or the next one?
這些表達是一樣的嗎? 哪個更有效率?
表達式 1:
$a = 'default'
if($b != "") $a = $b;
表達式 2:
$a = $b == "" ? "default" : $b;
$a在兩種情況下都保持相同的結果嗎?
您可以通過交互式運行在命令行中輕松測試小型 PHP 代碼段:
$ php -a
你可以在這里寫你的代碼:
$a = "test";
$b = $a;
$b = $b == "" ? "default" : $b;
var_dump($b);
$c = $a == "" ? "default" : $a;
var_dump($c);
應該是一樣的——但我會選擇表達式 2。我會這樣寫:
$this->a = ($r[0] == "") ? "default" : $r[0];
一切都取決於手頭的情況。 兩者都是有效的,但對於多種情況,這個更快,更有效
$a = $b == "" ? "default" : $b;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.