Linked Questions
57 questions linked to/from PHP short-ternary ("Elvis") operator vs null coalescing operator
551 votes
3 answers
377k views
What does double question mark (??) operator mean in PHP [duplicate]
I was diving into Symfony framework (version 4) code and found this piece of code: $env = $_SERVER['APP_ENV'] ?? 'dev'; I'm not sure what this actually does but I imagine that it expands to something ...
-2 votes
1 answer
1k views
what's difference between double sign question mark(??) and single question mark [duplicate]
I still don't understand how to read this type checking with symbols ?? and only single ? and what's the difference. When i want to check the value of data, for an example i try this code: //using ...
0 votes
2 answers
455 views
Is there a better way to write this sentence in PHP / Laravel? [duplicate]
I have this kind of test again, and again, and I feel like there is a more elegant way to do it. (isset($country['capital'])) ? $country['capital'] : null Can you help me?
3 votes
0 answers
1k views
PHP - Elvis Vs. Null coalesce [duplicate]
What is the difference between null coalesce operator and elvis? $a = null; echo $a ?: 'default1'; echo $a ?? 'default2'; It looks to me as they are doing the same job. However there must be ...
-1 votes
1 answer
494 views
How can I use Null coalescing ?? operator of php inside of an if statement with Laravel Eloquents to check object exists or not [duplicate]
How can I use Null coalescing ?? operator of PHP inside of an if statement with Laravel Eloquents to check an object exists or not? Currently, it is showing this error: SQLSTATE[42S22]: Column not ...
1 vote
2 answers
277 views
How do I prevent `undefined index` by using values already set in a database by default? [duplicate]
I am working on an admin panel for my website that will change website settings. Everything works the way it's supposed to, except for the number of PHP warnings I get when a submit some values. I get ...
0 votes
2 answers
100 views
Ternary operator versus null coalescing operator [duplicate]
How come I can use echo like this: <?php echo false ? 'yes' : 'no'; ?> //no But can't use it like this <?php echo false ?? 'yes'; ?> //nothing output
-2 votes
2 answers
129 views
What does the php operator "??" do? [duplicate]
$serial = ($_SERVER['SSL_CLIENT_M_SERIAL'] ?? false); It looks like a bit like a ternary. But for that I would have expected: $serial = $_SERVER['SSL_CLIENT_M_SERIAL'] ? $_SERVER['...
0 votes
0 answers
44 views
Php ternary operator - Am I using ?: correctly? [duplicate]
I just cannot figure what is going wrong so I am asking for help. This is my statement: $fs_ski_resorts_data[$i]['fs_ski_resort_aa_zoom'] = get_field('fs_ski_resort_data_fs_aa_zoom') ?: ...
0 votes
0 answers
34 views
PHP Operators ?? vs ?: [duplicate]
I recently read some PHP code and I've seen two operators : A ?? B operator which seems to mean take A if A is not null else take B A ?: B operator this one is the a shortcut for the ternary operator ...
0 votes
0 answers
23 views
PHP Ternary Operator not working as expected [duplicate]
I know there are countless articles out there to read about php ternary operators, however, I have read a bunch but cannot get it to work. This is my code without ternary operators: if ( get_field(...
63 votes
9 answers
35k views
Difference between period and comma when concatenating with echo versus return?
I just found that this will work: echo $value , " continue"; but this does not: return $value , " continue"; While . works instead of , in both the echo and return statements. ...
36 votes
10 answers
46k views
Overview of PHP shorthand
I've been programming in PHP for years now, but I've never learned how to use any shorthand. I come across it from time to time in code and have a hard time reading it, so I'd like to learn the ...
40 votes
4 answers
31k views
Is there a "nullsafe operator" in PHP?
Is there any way to write the following statement using some kind of safe navigation operator? echo $data->getMyObject() != null ? $data->getMyObject()->getName() : ''; So that it looks like ...
31 votes
1 answer
20k views
Difference between PHP ??, ?:, and ??=
Something I'm not exactly clear on, how does ??, ?:, and ??= differ in PHP? I know ??= was only added in PHP7.4. I also know that ?? is for checking null values, but ?: also seems to check for null ...