如何將 PHP 中的三元運算子 ( ? : ) 作為“if/else”的縮寫使用?


語法如下 −

$anyVariableName=yourCondition ? if condition becomes true then this will be executed: if condition becomes false then this gets executed;

範例

PHP 程式碼如下

 線上演示

<!DOCTYPE html>
<html>
<body>
<?php
   $value=100;
   $result=$value >=100 ? "Greater Than or Equal to 100":"Less Than 100";
   echo $result;
?>
</body>
</html>

輸出

將產生如下輸出

Greater Than or Equal to 100

更新於:13-Oct-2020

303 人瀏覽

啟動你的 職業

完成課程以獲得認證

開始
廣告
© . All rights reserved.