Shortest if/else using ternary operators - php

I have this piece of code:
<?=!empty($options["placeholder"]) ? $options["placeholder"]:'search...'?>
I was under the impression I could do like:
<?=!empty($options["placeholder"]) ?:'search...'?>
But when $options["placeholder"] is not empty then it returns 1, as it is a ternary operator.
Do I have to always issue the variable 2 times?

Yes. There is however been many requests wanting to change this:
https://wiki.php.net/rfc/ifsetor
https://wiki.php.net/rfc/isset-set-operator

If you can be sure that $options['placeholder'] will be set--if not you can prefix it with # to suppress the warning--you can drop the empty call:
<?= $options["placeholder"] ?: 'search...' ?>
The Elvis operator ?: evaluates to the left hand side if it is truthy (true when coerced to a boolean value such as 1 or a non-empty string 'foo') or the right hand side if not.

Related

Operator precedence of ?? operator

I am a bit confused about the precedence of PHP's ?? operator.
https://www.php.net/manual/en/language.operators.precedence.php says that it has lower precedence than the / (division) operator. If that is true, $a??1/0 should always give an error, not only if $a is Null, as it first evaluates 1/0. As it doesn't give a "division by zero" error, I assume that the two sides of the ?? operator are evaluated as two different expressions and the second is only evaluated if the first yields Null. While it absolutely makes sense to implement it this way, doesn't this contradict the precedence stated on above page?
The null coalescing ?? operator will evaluate the right part of the expression only if the left part is Null.
This means it will evaluate the left part first in order to know if it is worth evaluating the right part.
See this as an optimisation of the interpreter to prevent executing some code if it does not reach it, and it doesn't look like an operator precedence issue.
Same in an if statement, when if (expression=true), the following elseif will not be evaluated.
Referring in the link you provided in the question, consider the following when you code:
Use of parentheses, even when not strictly necessary, can often
increase readability of the code by making grouping explicit rather
than relying on the implicit operator precedence and associativity.
The example you provide
<?php
//$a=12;
$a=Null;
echo $a??1/0;
will really be parsed as:
<?php
//$a=12;
$a=Null;
echo ($a) ?? (1/0) ;
The extra parentheses will prevent the next developer that works on your code from getting it wrong.

Output String NOT Boolean Using Comparison OR || Operator

In JavaScript, I use the comparison "or" operator, || to check if a result is present. If not, print something else, e.g.:
console.log( '' || 'Yes' ); // Yes
In the above code snippet, "Yes" is printed.
However, in PHP, the behavior is different, e.g.:
<?php
echo '' || 'Yes'; // 1
?>
Instead of printing "Yes," PHP parses the boolean statement and it evaluates it as 1 (true).
In PHP, how can I achieve the same behavior as in JavaScript without using the ternary operator.
You may use the shorthand version of the ternary operator (even though that makes it no longer a ternary operator :)).
It will evaluate to its first operand if the condition is truthy, and fall back to the right operand if it's not:
echo '' ?: 'Yes'; // Yes
Demo (with a few examples): https://3v4l.org/ZXRE5
See this page in the manual for details as to what kind of values are considered falsy (everything else being therefore truthy).

How following script is evaluated?

<?php echo true?'what':true?'will':'print?';?>
Above code outputs will.
I am not able to get logic behind it. Can anyone explain it.
Thanks in advance.
From documentation:
Note:
It is recommended that you avoid "stacking" ternary expressions. PHP's behaviour when using more than one ternary operator within a single statement is non-obvious:
Example #4 Non-obvious Ternary Behaviour
// however, the actual output of the above is 't'
// this is because ternary expressions are evaluated from left to right
// the following is a more obvious version of the same code as above
echo ((true ? 'true' : false) ? 't' : 'f');
// here, you can see that the first expression is evaluated to 'true', which
// in turn evaluates to (bool)true, thus returning the true branch of the
// second ternary expression.
You should work with braces:
echo true?'what':(true?'will':'print?');
this will output what. The second if overrides the first if if there are no braces. Because ternary expressions get interpreted from left to right. So without any braces set by you PHPs interpreter will interpret your statement as:
echo (true?'what':true)?'will':'print?';
According to PHP.net you should avoid stacking ternary expressions:
It is recommended that you avoid "stacking" ternary expressions. PHP's behaviour when using more than one ternary operator within a single statement is non-obvious:
It is recommended that you avoid "stacking" ternary expressions. PHP's behaviour when using more than one ternary operator within a single statement is non-obvious: (as given in document)
Example:
`
on first glance, the following appears to output 'true'
echo (true?'true':false?'t':'f');
however, the actual output of the above is 't'
this is because ternary expressions are evaluated from left to right
the following is a more obvious version of the same code as above
echo ((true ? 'true' : false) ? 't' : 'f');
here, you can see that the first expression is evaluated to 'true', which
in turn evaluates to (bool)true, thus returning the true branch of the
second ternary expression.
`
The ternary operator in PHP is left-associative. Your code evaluates like this:
echo (true ? 'what' : true) ? 'will' : 'print?';
This is equivalent to:
echo (true) ? 'will' : 'print?';
And thus, the result is 'will'. You should use the following:
echo true ? 'what' : (true ? 'will' : 'print?');
A related post can be found here: Why is the output of `echo true ? 'a' : true ? 'b' : 'c';` 'b'?

Any more concise way to set default values?

Since PHP 5.3, it is possible to leave out the middle part of the
ternary operator. Expression expr1 ?: expr3 returns expr1 if expr1
evaluates to TRUE, and expr3 otherwise.
Is there any better or more concise way than following code to set default value of variables?
$v = isset($v) ? $v : "default value";
TL;DR - No, that expression can't be made any shorter.
What you want is for the shortened ternary expression to perform an implicit isset(). This has been discussed on the mailing list and an ifsetor RFC has been created that covers the concept as well.
Since the shortened ternary operator already existed at the time of the above discussion, something like this was proposed using a non-existent operator ??:
// PROPOSAL ONLY, DOES NOT WORK
$v = $v ?? 'default value';
Assign 'default value' if $v is undefined.
However, nothing like this has been implemented in the main language to date. Until then, what you have written can't be made any shorter.
This horrible construct is shorter, but note that it's not the same because it assigns the default value if the variable exists but evaluates to false:
// DO NOT USE
$v = #$v ?: 'default value';
Here is a shorter syntax:
isset($v) || $v="default value";
Just asked this and was pointed here. So in case you use a key of an array, this might be an improvement
function isset_get($array, $key, $default = null) {
return isset($array[$key]) ? $array[$key] : $default;
}
Nope. That's the right way if you don't really know whether $v is set.
No way.If you use ternary operator.

What are the PHP operators "?" and ":" called and what do they do?

What are the ? and : operators in PHP?
For example:
(($request_type == 'SSL') ? HTTPS_SERVER : HTTP_SERVER)
This is the conditional operator.
$x ? $y : $z
means "if $x is true, then use $y; otherwise use $z".
It also has a short form.
$x ?: $z
means "if $x is true, then use $x; otherwise use $z".
People will tell you that ?: is "the ternary operator". This is wrong. ?: is a ternary operator, which means that it has three operands. People wind up thinking its name is "the ternary operator" because it's often the only ternary operator a given language has.
I'm going to write a little bit on ternaries, what they are, how to use them, when and why to use them and when not to use them.
What is a ternary operator?
A ternary ? : is shorthand for if and else. That's basically it. See "Ternary Operators" half way down this page for more of an official explanation.
As of PHP 5.3:
Since PHP 5.3, it is possible to leave out the middle part of the ternary operator. Expression expr1 ?: expr3 returns expr1 if expr1 evaluates to TRUE, and expr3 otherwise.
As of PHP 7.0
PHP 7 has new Null Coalesce Operator. This is the same as a ternary but is also called an "isset ternary". This also allows a set of chained ternaries that remove the need for isset() checks.
In PHP 5, if you wanted to use a ternary with a potentially non-existent variable then you would have to perform an isset() at the beginning of the ternary statement:
$result = isset($nonExistentVariable) ? $nonExistentVariable : ‘default’;
In PHP 7, you can now do this instead:
$result = $nonExistentVariable ?? ‘default’;
The Null Coalesce Operator does not work with an empty string, however, so bear that in mind. The great thing about this is you can also chain the operators for multiple checks for multiple variables, providing a sort of backup depending on whether or not each variable in the chain exists:
$user = $userImpersonatingAnotherUser ?? $loggedInUser ?? “Guest”;
In PHP, with systems where a user can login, it is not uncommon for an administrator to be able to impersonate another user for testing purposes. With the above example, if the user is not impersonating another user, and also a logged in user does not exist, then the user will be a guest user instead. Read on more if you don't understand this yet to see what ternaries are and how they are used, and then come back to this bit to see how the new PHP
How are ternaries used?
Here's how a normal if statement looks:
if (isset($_POST['hello']))
{
$var = 'exists';
}
else
{
$var = 'error';
}
Let's shorten that down into a ternary.
$var = isset($_POST['hello']) ? 'exists' : 'error';
^ ^ ^ ^ |
| then | else |
| | |
if post isset $var=this $var=this
Much shorter, but maybe harder to read. Not only are they used for setting variables like $var in the previous example, but you can also do this with echo, and to check if a variable is false or not:
$isWinner = false;
// Outputs 'you lose'
echo ($isWinner) ? 'You win!' : 'You lose';
// Same goes for return
return ($isWinner) ? 'You win!' : 'You lose';
Why do people use them?
I think ternaries are sexy. Some developers like to show off, but sometimes ternaries just look nice in your code, especially when combined with other features like PHP 5.4's latest short echos.
<?php
$array = array(0 => 'orange', 1 => 'multicoloured');
?>
<div>
<?php foreach ($array as $key => $value) { ?>
<span><?=($value==='multicoloured')?'nonsense':'pointless'?></span>
<?php } ?>
</div>
<!-- Outputs:
<span>
pointless
</span>
<span>
nonsense
</span>
-->
Going off-topic slightly, when you're in a 'view/template' (if you're seperating your concerns through the MVC paradigm), you want as little server-side logic in there as possible. So, using ternaries and other short-hand code is sometimes the best way forward. By "other short-hand code", I mean:
if ($isWinner) :
// Show something cool
endif;
Note, I personally do not like this kind of shorthand if / endif nonsense
How fast is the ternary operator?
People LIKE micro-optimisations. They just do. So for some, it's important to know how much faster things like ternaries are when compared with normal if / else statements.
Reading this post, the differences are about 0.5ms. That's a lot!
Oh wait, no it's not. It's only a lot if you're doing thousands upon thousands of them in a row, repeatedly. Which you won't be. So don't worry about speed optimisation at all, it's absolutely pointless here.
When not to use ternaries
Your code should be:
Easy to read
Easy to understand
Easy to modify
Obviously this is subject to the persons intelligence and coding knowledge / general level of understanding on such concepts when coming to look at your code. A single simple ternary like the previous examples are okay, something like the following, however, is not what you should be doing:
echo ($colour === 'red') ? "Omg we're going to die" :
($colour === 'blue' ? "Ah sunshine and daisies" :
($colour === 'green' ? "Trees are green"
: "The bloody colour is orange, isn't it? That was pointless."));
That was pointless for three reasons:
Ridiculously long ternary embedding
Could've just used a switch statement
It was orange in the first place
Conclusion
Ternaries really are simple and nothing to get too worked up about. Don't consider any speed improvements, it really won't make a difference. Use them when they are simple and look nice, and always make sure your code will be readable by others in the future. If that means no ternaries, then don't use ternaries.
It's called a ternary operator. If the first expression evaluates to true, HTTPS_SERVER is used, else HTTP_SERVER is chosen.
It's basically a shorthand if statement, and the above code could also be rewritten as follows:
if ($request_type == 'SSL') {
HTTPS_SERVER;
}
else {
HTTP_SERVER;
}
This is sometimes known as the ternary conditional operator. Ternary means that it has three arguments, as x ? y : z. Basically, it checks if x is true; if it is, then put y instead of this operation, otherwise z.
$hello = $something ? "Yes, it's true" : "No, it's false";
Conditional operator ? : is an operator which is used to check a condition and select a value depending on the value of the condition. It is expressed in the following form:
variable = condition ? expression1 : expression2;
It works as follows...
Firstly, condition is evaluated.
If the condition is true, then expression1 is evalauated. And the value of expression1 is assigned to the variable.
If the condition is false, then expression2 is evaluated. And the value of expression2 is assigned to the variable.
For example:
x = (a>b) ? 5 : 9
In this, for x, firstly the condition (a>b) is evaluated. If this condition becomes true, then x will become the value 5 (ie, x=5). But if the condition (a>b) becomes false, then x will attain the value 9 (ie, x=9).
Ternary Operator
Sometimes conditional operator ? : is also called a ternary operator. This is so because it involves three operands. For example:
x ? y : z
Here, x,y and z are the three operands. If condition x is true, then value y is assigned otherwise value z is assigned.
This is a short way of writing if sentences. It is also used in other languages like Java, JavaScript and others.
Your code,
$protocol = $request_type == 'SSL' ? HTTPS_SERVER : HTTP_SERVER;
can be written like this:
if ($request_type == 'SSL')
$protocol = HTTPS_SERVER;
else
$protocol = HTTP_SERVER;
That is a one line if statement:
condition ? true : false
Translated to an ordinary if statement in your case, that would be:
if($request_type == 'SSL') HTTPS_SERVER;
else HTTP_SERVER;
That's basically a fancy way of writing an if-else statement. Some say it's easier to read, some say not.
Ternary operator at Wikipedia
This works like an if statement it's very simple and easy once you get used to it.
(conditions_expressions) ? what_to_do_if_true : what_to_do_if_false.
As John T says, it is called a ternary operator and is essentially a shorthand version of an if /else statement. Your example, as a full if / else statement, would read;
if($request_type == 'SSL')
{
HTTPS_SERVER;
}
else
{
HTTP_SERVER;
}

Categories