How does the ternary operator differ from an if statement in PHP?

The ternary operator in PHP is a shorthand way of writing an if-else statement. It is typically used for simple conditional assignments or expressions where you have one condition to check. The ternary operator is more concise and can make your code more readable in certain situations compared to using an if statement.

// Using a ternary operator to assign a value based on a condition
$age = 25;
$is_adult = ($age >= 18) ? true : false;

echo $is_adult ? 'You are an adult' : 'You are not an adult';