This is called a ternary operator. It is presented in this form :
condition ? value_if_condition_true : value_if_condition_false;
For example you can use the result of this expression for an assignment, let's say :
$load_page = is_user_logged_in() ? true : false;
In fact, this is the equivalent of writing :
if (is_user_logged_in())
$load_page = true;
else
$load_page = false;
EDIT Because I love the ternary operator, I want to write more.
This operator form has no effect on performance or behaviour compared to the classic else/if format, and only serves the purpose of being an elegant one liner. (Which is the reason why some languages don't implement it, taking the view that it is unnecessary and often bad practice to have N ways of writing the same instruction)
But to show how elegant it is, imagine you know the age of a user, and the age_of_subscription to your website, and you want to display a message depending on these two variables according to these rules :
- if the user is older than 20 and had a subscription for more than 3 year, he is allowed to a discount
- if the user is younger than 20 and had a subscription for more than 1 years, he is allowed to a discount
- in any other case, the user is paying the full price.
In the classic IF/ELSE form, you would write :
if ($age > 20)
{
$text = "You are above 20 and you have been a user for ";
if ($age_of_subscription > 3)
{
$text .= "more than 3 years so you are allowed a discount";
}
else
{
$text .= "less than 3 years so you are paying full price";
}
}
else
{
$text = "You are below or 20 and you have been a user for ";
if ($age_of_subscription > 1)
{
$text .= "more than 1 year so you are allowed a discount";
}
else
{
$text .= "less than 1 year so you are paying full price";
}
}
echo $text;
But now, watch the beauty of ternary operator :
echo 'You are ',
($age > 20 ?
($age_of_subscription > 3 ? 'above 20 and you have been a user for more than 3 years
\ so you are allowed a discount' : 'above 20 and you have been a user for less than 3 years
\ so you are paying full price.')
: ($age_of_subscription > 1 ? 'below or 20 and you have been a user for more than 1 year
\ so you are allowed a discount' : 'below or 20 and you have been a user for less than 1 year
\ so you are paying full price.')); // pretty uh?