Twig ternary operator, Shorthand if-then-else
In PHP, you can use the ternary operator to perform a shorthand if-then-else statement. The syntax for the ternary operator is as follows:
<?php
$age = 25;
$result = $age >= 18 ? 'You are an adult.' : 'You are not an adult.';
echo $result;
The condition
is evaluated first. If it is true
, the true_value
is assigned to the $result
variable. If it is false
, the false_value
is assigned to the $result
variable.
Here is an example of how you can use the ternary operator in PHP:
<?php
$is_logged_in = true;
$result = ($is_logged_in) ? 'You are logged in' : 'You are not logged in';
echo $result;
This will output the following string:
You are logged in