Computer >> Máy Tính >  >> Lập trình >> PHP

Làm cách nào để sử dụng toán tử bậc ba (?:) Trong PHP làm cách viết tắt cho “if / else”?

Cú pháp như sau -

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

Ví dụ

Mã PHP như sau

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

Đầu ra

Điều này sẽ tạo ra kết quả sau

Greater Than or Equal to 100