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).
Related
<?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'?
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.
I see a lot of people using a variety of different methods to check whether of a variable is empty, there really seems to be no consensus. I've heard that if($foo) is exactly the same as if(!empty($foo)) or if($foo != ""). Is this true?
I realize it's a really simple question, but I'd really like to know. Are there any differences? Which method should I use?
Difference between bare test and comparison to empty string
if($foo != "") is equivalent to if($foo) most of the time, but not always.
To see where the differences are, consider the comparison operator behavior along with the conversion to string rules for the first case, and the conversion to boolean rules for the second case.
What I found out is that:
if $foo === array(), the if($foo != "") test will succeed (arrays are "greater than" strings), but the if($foo) test will fail (empty arrays convert to boolean false)
if $foo === "0" (a string), the if($foo != "") test will again succeed (obviously), but the if($foo) test will fail (the string "0" converts to boolean false)
if $foo is a SimpleXML object created from an empty tag, the if($foo != "") test will again succeed (objects are "greater than" strings), but the if($foo) test will fail (such objects convert to boolean false)
See the differences in action.
The better way to test
The preferred method to test is if(!empty($foo)), which is not exactly equal to the above in that:
It does not suffer from the inconsistencies of if($foo != "") (which IMHO is simply horrible).
It will not generate an E_NOTICE if $foo is not present in the current scope, which is its main advantage over if($foo).
There's a caveat here though: if $foo === '0' (a string of length 1) then empty($foo) will return true, which usually is (but may not always be) what you want. This is also the case with if($foo) though.
Sometimes you need to test with the identical operator
Finally, an exception to the above must be made when there is a specific type of value you want to test for. As an example, strpos might return 0 and also might return false. Both of these values will fail the if(strpos(...)) test, but they have totally different meanings. In these cases, a test with the identical operator is in order: if(strpos() === false).
No it's not always true. When you do if($foo) PHP casts the variable to Boolean. An empty string, a Zero integer or an empty array will then be false. Sometimes this can be an issue.
You should always try to use the most specific comparison as possible, if you're expecting a string which could be empty use if($foo==='') (note the three equal signs). If you're expecting either (boolean) false or a resource (from a DB query for instance) use if($foo===false){...} else {...}
You may read the documentation about casting to boolean to find the answer to this question. There's a list in there with which values are converted to true and false, respectively.
Note that empty also checks if the variable is set, which regular comparison does not. An unset variable will trigger an error of type E_NOTICE during comparison, but not when using empty. You can work around this using the isset call before your comparison, like this:
if(isset($foo) && $foo != '')
if() "converts" the statement given to a bool, so taking a look at the documentation for boolean seems to be what you're looking for. in general:
empty strings (""), empty arrays (array()), zero (0) and boolean false (false) are treated as false
everything else ("foo", 1, array('foo'), true, ...) is treated as true
EDIT :
for more information, you could also check the type comparison tables.
empty($foo) should return true in all of these cases: 0,"", NULL.
For a more complete list check this page: http://php.net/manual/en/function.empty.php
No, it's not equal. When variable is not defined, expression without empty will generate notice about non-defined variable.
I was wondering how php processes if statements.
If i were to have something such as:
if (isset($_GET['foo']) && $_GET['foo'] != $bar)
If foo isn't set, would it then drop out of the if straight away (as it is an 'and' statement so it can't succeed anyway) or would it also check the second part, rather pointlessly?
What you're describing is known as "short-circuit evaluation".
Most languages work this way, including PHP, so they will evaluate an expression until they are certain of the result, and then stop, so the remainder of the expression would not be evaluated.
As you say, this is the most efficient approach.
However, it can potentially throw a spanner in the works for inexperienced programmers, who nay try something like this:
if(doFirstProcess() && doSecondProcess() {
print "both processes succeeded";
}
In this case, the programmer is expecting both functions to be called, but if the first one returns false, then the second one will not be executed, as the program already knows enough to be certain of the final result of the expression, so it short-circuits the remainder of the expression.
There are a few languages which don't do short-circuit evaluation. VB6 was one example (back in the day). I don't know about VB.Net, but since it's evolved from VB6, I would suspect it would be similar. But aside from that, all other languages that I've worked with have used short-circuit evaluation, including PHP.
There is a section in the PHP manual about this here: http://www.php.net/manual/en/language.operators.logical.php
And you can read more on short circuit evalution here: http://en.wikipedia.org/wiki/Short-circuit_evaluation
Hope that helps.
It's known as short-circuit:
&& and and operators is executed from left to side
If left side is considered false, no reasons to check right side, so it's omitted, false returned
|| and or operators is executed from left to side too
If left side is considered true, no reasons to check right side, so it's omitted, true returned
Manual example:
// foo() will never get called as those operators are short-circuit
$a = (false && foo());
$b = (true || foo());
$c = (false and foo());
$d = (true or foo());
it will leave the if statement after the first expression evaluates to false because this statement can never be true if the first one is false and they are combinded via AND
you can check this very easily. If it wouldn't be like I said, you would get a notice that $_GET['foo'] is not defined
If the first part is false, it stops the if.
Certain operators, most notably && and || are so-called short-circuit operators, meaning that if the result of the operation is clear from the first operand (false or true, respectively), the second operand does not get evaluated.
Edit: Additionally, operands are guaranteed to be evaluated in order, this is not always true of other operators.
Will go out after the first statement.
you can test it:
will print 1:
if(1==1 && $a=1 == 1){
}
print $a;
Will not print a thing:
if(1==2 && $a=1 == 1){
}
print $a;
&& does short-circuit (i.e. returns false as soon as one condition fails).
If it doesn't, then having the isset would be pointless — it exists to prevent errors when trying to compare an undefined value to a string.
If the first check if (isset($_GET['foo']) returns false, the second part will not even be looked into anymore.
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;
}