![](/img/trans.png)
[英]What is the difference between Switch-Case and If-Else in PHP?
[英]What are the difference between PHP 8 Match expression vs PHP 7 switch case?
PHP 8 匹配表達式代碼
echo match (8.0) {
'8.0' => "Oh no!",
8.0 => "This is what I expected",
};
//> This is what I expected
PHP 7 開關代碼
switch (8.0) {
case '8.0':
$result = "Oh no!";
break;
case 8.0:
$result = "This is what I expected";
break;
}
echo $result;
//> Oh no!
主要區別:
如果您想了解更多,匹配表達式已經在 PHP 文檔中找到了它的頁面: https://www.php.net/manual/en/control-structures.match.ZE1BFD762321E409CEE4AC0B6E84193CZZAC0B6E84196
匹配表達式基於值的身份檢查分支評估。 與 switch 語句類似,匹配表達式具有與多個備選方案進行比較的主題表達式。 與 switch 不同,它的計算結果很像三元表達式。 與 switch 不同,比較是身份檢查 (===) 而不是弱相等檢查 (==)。 匹配表達式自 PHP 8.0.0 起可用。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.