Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> PHP

การใช้ตัวดำเนินการ ternary (?:) ใน PHP แทน if/else

ไวยากรณ์มีดังนี้

$anyVariableName = yourCondition ? value_if_true : value_if_false;

ตัวอย่างโค้ด

โค้ด 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