PHP double question marks (Null coalescing operator) explained

✋ Update: This post was originally published on my blog decodingweb.dev, where you can read the latest version for a 💯 user experience. ~reza

PHP double question marks (??) – officially known as Null coalescing operator – is a convenient alternative…


This content originally appeared on DEV Community 👩‍💻👨‍💻 and was authored by Reza Lavarian

Update: This post was originally published on my blog decodingweb.dev, where you can read the latest version for a 💯 user experience. ~reza

PHP double question marks (??) – officially known as Null coalescing operator – is a convenient alternative to ternary expressions in conjunction with isset().

You might have seen the following expression in your Laravel or PHP projects:

<?php

$result = $value ?? $alternativeValue;

But what do two question marks mean in PHP?

The above expression returns its first operand ($value) if it exists and is not null; otherwise, it returns the second operand ($alternativeValue).

The above expression is equivalent to:

<?php

$result = isset($value) ? $value : $alternativeValue;

How to use PHP double question marks

Before the Null coalescing operator, you'd have to use a ternary operator with a call to isset():

<?php

$iceCreamFlavor = isset($_POST['flavor']) ? $_POST['flavor'] : 'vanilla';

Or as an if statement:

<?php

$iceCreamFlavor = 'vanilla';

if (isset($_POST['flavor'])) {
  $iceCreamFlavor = $_POST['flavor'];
}

But with the Null coalescing operator, you can summarize it into:

<?php

$iceCreamFlavor =$_POST['flavor'] ?? 'vanilla';

You can also chain multiple operators:

<?php

$display_name =  $first_name ?? $last_name ?? 'Anonymous';

The above code will return the first defined value among $first_name, $last_name, and Anonymous.

The Null coalescing operator has been added to PHP since version 7.

I hope you found this quick tip helpful :-)

Thanks for reading!

❤️ You might like:


This content originally appeared on DEV Community 👩‍💻👨‍💻 and was authored by Reza Lavarian


Print Share Comment Cite Upload Translate Updates
APA

Reza Lavarian | Sciencx (2023-02-06T18:51:01+00:00) PHP double question marks (Null coalescing operator) explained. Retrieved from https://www.scien.cx/2023/02/06/php-double-question-marks-null-coalescing-operator-explained/

MLA
" » PHP double question marks (Null coalescing operator) explained." Reza Lavarian | Sciencx - Monday February 6, 2023, https://www.scien.cx/2023/02/06/php-double-question-marks-null-coalescing-operator-explained/
HARVARD
Reza Lavarian | Sciencx Monday February 6, 2023 » PHP double question marks (Null coalescing operator) explained., viewed ,<https://www.scien.cx/2023/02/06/php-double-question-marks-null-coalescing-operator-explained/>
VANCOUVER
Reza Lavarian | Sciencx - » PHP double question marks (Null coalescing operator) explained. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2023/02/06/php-double-question-marks-null-coalescing-operator-explained/
CHICAGO
" » PHP double question marks (Null coalescing operator) explained." Reza Lavarian | Sciencx - Accessed . https://www.scien.cx/2023/02/06/php-double-question-marks-null-coalescing-operator-explained/
IEEE
" » PHP double question marks (Null coalescing operator) explained." Reza Lavarian | Sciencx [Online]. Available: https://www.scien.cx/2023/02/06/php-double-question-marks-null-coalescing-operator-explained/. [Accessed: ]
rf:citation
» PHP double question marks (Null coalescing operator) explained | Reza Lavarian | Sciencx | https://www.scien.cx/2023/02/06/php-double-question-marks-null-coalescing-operator-explained/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.