What does double question mark (??) operator mean in PHP
It's the "null coalescing operator", added in php 7.0. The definition of how it works is:
It returns its first operand if it exists and is not NULL; otherwise it returns its second operand.
So it's actually just isset()
in a handy operator.
Those two are equivalent1:
$foo = $bar ?? 'something';
$foo = isset($bar) ? $bar : 'something';
Documentation: http://php.net/manual/en/language.operators.comparison.php#language.operators.comparison.coalesce
In the list of new PHP7 features: http://php.net/manual/en/migration70.new-features.php#migration70.new-features.null-coalesce-op
And original RFC https://wiki.php.net/rfc/isset_ternary
EDIT: As this answer gets a lot of views, little clarification:
1There is a difference: In case of ??
, the first expression is evaluated only once, as opposed to ? :
, where the expression is first evaluated in the condition section, then the second time in the "answer" section.
What are the ?? double question marks in Dart?
The ??
double question mark operator means "if null". Take the following expression, for example.
String a = b ?? 'hello';
This means a
equals b
, but if b
is null then a
equals 'hello'
.
Another related operator is ??=
. For example:
b ??= 'hello';
This means if b
is null then set it equal to hello
. Otherwise, don't change it.
Reference
- A Tour of the Dart Language: Operators
- Null-aware operators in Dart
Terms
The Dart 1.12 release news collectively referred to the following as null-aware operators:
??
-- if null operator??=
-- null-aware assignmentx?.p
-- null-aware accessx?.m()
-- null-aware method invocation
Try Catch using double question mark(??) in laravel view(blade) does'nt work
The double question mark, also called null coalescing operator is, in your case, ran after the == operation. Soo if you want to achieve this, you need to put parenthesis like that:($clr->id == ($data->classroom??''))[...]
PHP syntax question: What does the question mark and colon mean?
This is the PHP ternary operator (also known as a conditional operator) - if first operand evaluates true, evaluate as second operand, else evaluate as third operand.
Think of it as an "if" statement you can use in expressions. Can be very useful in making concise assignments that depend on some condition, e.g.
$param = isset($_GET['param']) ? $_GET['param'] : 'default';
There's also a shorthand version of this (in PHP 5.3 onwards). You can leave out the middle operand. The operator will evaluate as the first operand if it true, and the third operand otherwise. For example:
$result = $x ?: 'default';
It is worth mentioning that the above code when using i.e. $_GET or $_POST variable will throw undefined index notice and to prevent that we need to use a longer version, with isset
or a null coalescing operator which is introduced in PHP7:
$param = $_GET['param'] ?? 'default';
php - Meaning of question mark colon operator
It is a shorthand for an if
statement.
$username = $_COOKIE['user'] ?: getusername($_COOKIE['user']);
Is the same as
if( $_COOKIE['user'] )
{
$username = $_COOKIE['user'];
}
else
{
$username = getusername($_COOKIE['user']);
}
see test suite here: https://3v4l.org/6XMc4
But in this example, the function 'getusername' probably doesn't work correct, because it hits the else
only when $_COOKIE['user']
is empty
. So, the parameter inside getusername()
is also kind of empty.
PHP: is there a good syntax to initialize an array element depending on whether it is undefined or not, without using if?
The array_filter()
method only returns the non-empty values from an array by default.
This code shows the various outcomes:
$bar1=1;
$fooArr = [$bar1, $bar2 ?? null];
print_r($fooArr);
$bar2=2;
$fooArr = [$bar1, $bar2 ?? null];
print_r($fooArr);
unset($bar1,$bar2);
$bar1=1;
$fooArr = array_filter([$bar1, $bar2 ?? null]);
print_r($fooArr);
$bar2=2;
$fooArr = array_filter([$bar1, $bar2 ?? null]);
print_r($fooArr);
Teh playground
What does the question mark as well as the colon (:) do in php, particularly in this line?
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?
Related Topics
Html,PHP - Escape '<' and '>' Symbols While Echoing
How to Easily Switch Between PHP Versions on MAC Osx
Generating a Random Hex Color Code with PHP
Converting a Number Base 10 to Base 62 (A-Za-Z0-9)
Return the Portion of a String Before the First Occurrence of a Character in PHP
How to Send a Status Code in PHP, Without Maintaining an Array of Status Names
MySQL (Or PHP) Group Results by Field Data
Php: Variable Not Working Inside of Function
Sanitizing User's Data in Get by PHP
Artisan Migrate Could Not Find Driver
PHP Conditionals, Brackets Needed
How to Check If a Request If Coming from the Same Server or Different Server
Magic _Get Getter for Static Properties in PHP