PHP Shorthand If Statement

The shorthand if statement is used to execute a block of code if a condition is true.

Syntax

(condition) ? true_value : false_value;

Example of PHP Shorthand If Statement

<?php
$x = 10;
$y = ($x > 5) ? 'x is greater than 5' : 'x is less than or equal to 5';
echo $y;
?>

Output:

x is greater than 5

Explanation: This example demonstrates the use of the shorthand if statement in PHP. If the condition is true, the value 'x is greater than 5' is assigned to $y. If the condition is false, the value 'x is less than or equal to 5' is assigned to $y.