In an online tutorial I have seen the following snippet of code:
$this->data = $data ?: \Input::all();
Is this a standard ternary operator? What would happen when $data evaluates to true?
Does the following code do the same thing as the original I posted?
$this->data = $data ? null : \Input::all();
It's a ternary operator, shortcut of
$this->data = $data? $data : \Input::all();
From http://php.net/manual/en/language.operators.comparison.php
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.
Related
Is there a way to prevent using if to check if an element exists before append ing itto a string, so it does not cause an error?
e.g.
$output = "";
if($data['element'])
{
$output .= $data['element'];
}
in Javascript I can use:
var output = "";
output += data['element'] || '';
You could always use the Ternary Operator ?:
$output .= (isset($data['element'])) ? $data['element'] : '';
From the PHP Docs
The expression (expr1) ? (expr2) : (expr3) evaluates to expr2 if expr1 evaluates to TRUE, and expr3 if expr1 evaluates to FALSE.
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.
use isset to check it.
$output = "";
if(isset($data['element']))
{
$output .= $data['element'];
}
Use the PHP Ternary Operator like this:
(isset($data['element'])) ? $output=$data['element'] : $output="";
I am having issue with PHP's ternary operator, since PHP version 5.3 you are able to replace the shorthand ternary operator with an even shorter version
// Older version
$route = isset($test) ? $test : 'test is NOT set';
// Newer version as of 5.3
$route = isset($test) ?: 'test is NOT set';
Now on the newer version, if $test is not set. it works fine. However when it is set because of the isset() method, it is returning true or 1 instead of the value.
Do I have to use the older longer method to get $route to equal the value of $test instead of a boolean value of 1 ?
You have to use the longer version.
Quoting the docs:
Expression expr1 ?: expr3 returns expr1 if expr1 evaluates to TRUE, and expr3 otherwise.
So the correct behaviour for your shorthand version is to return the result of evaluating isset($test). Not of $test as you want.
Starting from PHP 7, you can use the null coalescing operator:
$route = $test ?? 'test is NOT set';
which is equivalent to
$route = isset($test) ? $test : 'test is NOT set';
Here you can find some details:
The null coalescing operator (??) has been added as syntactic sugar for the common case of needing to use a ternary in conjunction with isset(). It returns its first operand if it exists and is not NULL; otherwise it returns its second operand.
Given the following expression:
$att['menutext'] = isset($attrib_in['i_menu_text']) ? : $this->getID();
If it evaluates to true, will $att['menutext'] be set to true or $this->getID()?
According to this reference:
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.
Yes, in version 5.3+ the middle expression is optional and returns true.
$a = (true ? : 1); // $a evaluates to true.
$a = (false ? : 1); // $a evaluates to 1.
It's just the same as the following
$att['menutext'] = isset($attrib_in['i_menu_text']) ? true : $this->getID();
never tested before, but its quite easy to test:
<?php var_dump(TRUE ? : 'F'); ?>
and its says: bool(true)
This won't execute, it's invalid syntax for PHP < 5.3.
Parse error: syntax error, unexpected ':' on line X
If you want the value to be set to true, then use true:
$att['menutext'] = isset($attrib_in['i_menu_text']) ? true : $this->getID();
Or it may be more likely that you want:
$att['menutext'] = isset($attrib_in['i_menu_text']) ? $attrib_in['i_menu_text'] : $this->getID();
what does this structure mean:
$var = isset($var_1) ? $var_1 : $var_2;
I came across it and of course with other values, not $va, $var_1 and $var_2.
thanks.
This is the ternary operator, and means the same as:
if (isset($var_1)) {
$var = $var_1;
}
else {
$var = $var_2;
}
The ternary operator provides a short-hand method of creating simple if/else statements.
It has some syntax errors, correctly:
$var = isset($var_1) ? $var_1 : $var_2;
This means:
if (isset($var_1))
{
$var = $var_1;
}
else
{
$var = $var_2;
}
That means:
if(isset($var_1))
$var = $var_1;
else
$var = $var_2;
It is short syntax for that.
just for your information from the php manual i copy pasted good things to know about ternary comparision operators
The expression (expr1) ? (expr2) : (expr3) evaluates to expr2 if expr1 evaluates to TRUE, and expr3 if expr1 evaluates to FALSE.
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.
Note: Please note that the ternary operator is a statement, and that it doesn't evaluate to a variable, but to the result of a statement. This is important to know if you want to return a variable by reference. The statement return $var == 42 ? $a : $b; in a return-by-reference function will therefore not work and a warning is issued in later PHP versions.
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 #3 Non-obvious Ternary Behaviour
<?php
// 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.
?>
Simple question. Here is this code.
$r = rand(0,1);
$c = ($r==0)? rand(65,90) : rand(97,122);
$inputpass .= chr($c);
I understand what it does in the end result, but I'd like a better explanation on how it works, so I can use it myself. Sorry if this is a bad question.
If you're unsure of what I'm asking about, its the (function?) used here:
$c = ($r==0)? rand(65,90) : rand(97,122);
That's called a ternary operator. It's effectively the equivalent of
if ($r == 0) {
$c = rand(65, 90);
} else {
$c = rand(97, 122);
}
But it's obviously a bit more compact. Check out the docs for more info.
That simply means:
if($r==0){
$c = rand(65,90);
else{
$c = rand(97,122);
}
If the statement is true the first operation after that ? is executed, else the operation after : is executed.
Its called a ternary operator.
Its the Ternary Operator
<?php
// Example usage for: Ternary Operator
$action = (empty($_POST['action'])) ? 'default' : $_POST['action'];
// The above is identical to this if/else statement
if (empty($_POST['action'])) {
$action = 'default';
} else {
$action = $_POST['action'];
}
?>
The expression (expr1) ? (expr2) : (expr3) evaluates to expr2 if expr1 evaluates to TRUE, and expr3 if expr1 evaluates to FALSE.
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.
It's called the ternary operator. It's similar to an if/else construct, but the difference is that an expression using the ternary operator yields a value.
That is, you can't set a variable to the result of an if/else construct:
// this doesn't work:
$c = if ($r == 0) {
rand(65, 90);
} else {
rand(97, 122);
}
You can read more about it here: http://php.net/ternary#language.operators.comparison.ternary
The ternary operator is frequently misused. There's little benefit to using it in the example you showed. Some programmers love to use compact syntactic sugar even when it's not needed. Or even when it's more clear to write out the full if/else construct.
The ternary operator can also obscure test coverage if you use a tool that measures lines of code covered by tests.