This question already has answers here:
Reference Guide: What does this symbol mean in PHP? (PHP Syntax)
(24 answers)
Closed 4 years ago.
While I was looking over symfony's public/index.php I came across the following snippet of code:
$env = $_SERVER['APP_ENV'] ?? 'dev';
$debug = (bool) ($_SERVER['APP_DEBUG'] ?? ('prod' !== $env));
Therefore I performed the following searches:
https://duckduckgo.com/?q=php+%3F%3F&t=canonical&ia=web
https://duckduckgo.com/?q=php+%24var+%3F%3F+%24var2&t=canonical&ia=qa
https://secure.php.net/manual-lookup.php?pattern=%3F%3F&scope=quickref
But still I cannot figure out what the operator ?? actually means. Can you provide me info regarding this operator/syntax ?
it's shortest version of
$env = isset($_SERVER['APP_ENV']) ? $_SERVER['APP_ENV'] : 'dev';
was added in PHP 7
Related
This question already has answers here:
Reference Guide: What does this symbol mean in PHP? (PHP Syntax)
(24 answers)
Closed 4 days ago.
I have just had a wordpress template fail giving E_PARSE errors on a number of lines with the following syntax:
$thumbnail_size = $post->image_size ?? 'medium';
I am not familiar with the ?? operator so replaced the lines with this:
$thumbnail_size = $post->image_size ? $post->image_size : 'medium';
Is this an equivalent statement?
Is there any reason why ?? should fail? (PHP Version 5.6.40, Wordpress 6.1.1)
The original code was working 3 days ago. Wordpress version hasn't changed and PHP Info doesn't suggest the PHP version has changed for over 6 months.
The null coalescing operator ?? was introduced in PHP 7.0.
$x = $a ?? $b is equivalent to $x = isset($a) ? $a : $b. If $a is a complex expression (e.g. a function call), it is evaluated only once.
So no, your two statements are not equivalent.
This question already has answers here:
Reference Guide: What does this symbol mean in PHP? (PHP Syntax)
(24 answers)
What does "===" mean? [duplicate]
(10 answers)
Closed 3 years ago.
I saw the line below in a class method :
return $return === 1;
What's the meaning of this ? As "===" is a comparaison operator, what would be the expected behavior of using this operator directly in a return ? Return only if 1 ?
Thanks
This question already has answers here:
Reference Guide: What does this symbol mean in PHP? (PHP Syntax)
(24 answers)
Closed 4 years ago.
what is the meaning of '??' operator in php?
I've seen someone using it like this. I've searched many results in google. But didn't found any solution.
$this->variable = $arg['value'] ?? NULL
?? is the new (since PHP 7) NULL coalescing operator. If the first argument is set and is not null it is returned, otherwise, the second argument is returned.
This question already has answers here:
Reference Guide: What does this symbol mean in PHP? (PHP Syntax)
(24 answers)
Closed 5 years ago.
I've seen this code and I don`'t know what does it mean
$page = $_GET['page'] ?? 'home';
Can somebody tell me its meaning?
It's null coalesce operator.
It will return $_GET['page'] unless it's null. In case it's null it would return default value 'home'.
It has same meaning as:
!is_null($_GET['page']) ? $_GET['page'] : 'home'
This question already has answers here:
Stacking Multiple Ternary Operators in PHP
(11 answers)
Closed 6 years ago.
I'm trying to display a correct results based on the value of the variable but I'm using a short hand,
I have tried the following but its seems to ignore the first check even if the value is correct.
(!empty($national_ID_number)) ?
$national_ID_number :
(!empty($foreign_ID_number)) ?
$foreign_ID_number :
$temporal_permit_number)
Thanks
It's populating correctly for me with this code, you seem to have added additional bracket.
$national_ID_number = '';
$foreign_ID_number = 2;
$temporal_permit_number = 3;
$finalID = (!empty($national_ID_number)) ? $national_ID_number : (!empty($foreign_ID_number ) ? $foreign_ID_number : $temporal_permit_number);
echo $finalID;
I just went for a bit deep research - apparently each condition needs to be inside brackets and the below fix works fine.
(!empty($national_ID_number)) ?
$national_ID_number :
((!empty($foreign_ID_number)) ?
$foreign_ID_number :
$temporal_permit_number));
Reference and PHP Operator precedence