For a template engine I would like to use shorthand if condition. I need to check if a value is != null print out some line if true. What I tried:
echo "test" ($user->affiliate_id !=null) ?
I have no idea what to write behind the ?.
For a template engine I would like to use shorthand if condition. I need to check if a value is != null print out some line if true. What I tried:
echo "test" ($user->affiliate_id !=null) ?
I have no idea what to write behind the ?.
On
Syntax should be like this
echo (condition) ? write if true code part here : write else part here
that means in your case it will be like
echo ($user->affiliate_id !=null) ? 'test' : 'not null'
On
As mentioned by @Federinco it's called the ternary operator, the official docs are here: http://php.net/manual/en/language.operators.comparison.php#language.operators.comparison.ternary
The ternary operator works like so:
condition ? true branch : false branch
So an example would be:
$cost = isset($discount) ? $total - $discount : $total;
In the above example we check if discount has been set, and if it has we removed the discount from our current total, if not we just set the cost to the total.
There are other cool things you can do with the ternary operator like so:
$name = $username ?: 'Guest';
In the above code we are checking whether $username exists, if it is we set $name to $username otherwise we set $name equal to Guest
A word of caution though, the ternary operator can lead to readability issues if you abuse it, so be careful where you use it and don't sacrifices the extra lines of a standard conditional statement if the ternary operator is not going to be clear in what it is doing.
On
What you want is the ternary operator
Your code should look like this
echo "test" . (($user->affiliate_id !=null) ? 'stringToOutput ifNotNull' : 'stringToOutput if is null');
Also, PHP 7 has introduced the Null coalescing operator which you could use. You can do this like this
echo 'test' . ($user->affiliate_id ?? 'ID not found!');
In which case, if $user->affiliate_id is set and not null, it will be printed instead of the 'ID not found!' message
The line
$someVariable = $condition ? $valueA : $valueBis equivalent to:So, basically, if the condition is
TRUE,$someVariablewill take the first value after the?symbol. IfFALSE, it will take the second value (the one after the:symbol).There's a special case where you can not define the first value and it is like this:
$someVariable = $someValue ?: $someOtherValue. It's equivalent to:So, if
$someValueevaluates toTRUE(any value different than0is evaluated toTRUE), then$someVariablewill catch that value. Otherwise, it will catch$someOtherValue.To give you an example:
Another example:
EDIT:
The operator
?:is NOT called theternary operator. There are multiple ways to define a ternary operator (an operator that takes three operands). It is a ternary operator, but not the ternary operator. Some people just call it the ternary operator because they're used to do it and, probably, it's the only ternary operator vastly known in PHP, but a ternary operator is way more general than that.It's name is the conditional operator or, more strictly, the ternary conditional operator.
Let's suppose I define a new operator called
log basethat evaluates, inline, if the logarithm of a number$Awith base$Cequals to$B, with its syntax like$correct = $A log $B base $Cand it returnsTRUEif the logarithm is right, orFALSEif it's not.Of course that operation is hypothetical, but it is a ternary operator, just like
?:. I'm gonna call it the logarithm checker operator.