Notice: Function _load_textdomain_just_in_time was called incorrectly. Translation loading for the astra-sites domain was triggered too early. This is usually an indicator for some code in the plugin or theme running too early. Translations should be loaded at the init action or later. Please see Debugging in WordPress for more information. (This message was added in version 6.7.0.) in /home/u162105626/domains/phptechtutor.com/public_html/wp-includes/functions.php on line 6121

Notice: Function _load_textdomain_just_in_time was called incorrectly. Translation loading for the rank-math domain was triggered too early. This is usually an indicator for some code in the plugin or theme running too early. Translations should be loaded at the init action or later. Please see Debugging in WordPress for more information. (This message was added in version 6.7.0.) in /home/u162105626/domains/phptechtutor.com/public_html/wp-includes/functions.php on line 6121

Notice: Function _load_textdomain_just_in_time was called incorrectly. Translation loading for the rank-math domain was triggered too early. This is usually an indicator for some code in the plugin or theme running too early. Translations should be loaded at the init action or later. Please see Debugging in WordPress for more information. (This message was added in version 6.7.0.) in /home/u162105626/domains/phptechtutor.com/public_html/wp-includes/functions.php on line 6121

Notice: Function _load_textdomain_just_in_time was called incorrectly. Translation loading for the wpforms-lite domain was triggered too early. This is usually an indicator for some code in the plugin or theme running too early. Translations should be loaded at the init action or later. Please see Debugging in WordPress for more information. (This message was added in version 6.7.0.) in /home/u162105626/domains/phptechtutor.com/public_html/wp-includes/functions.php on line 6121

Notice: Function _load_textdomain_just_in_time was called incorrectly. Translation loading for the astra domain was triggered too early. This is usually an indicator for some code in the plugin or theme running too early. Translations should be loaded at the init action or later. Please see Debugging in WordPress for more information. (This message was added in version 6.7.0.) in /home/u162105626/domains/phptechtutor.com/public_html/wp-includes/functions.php on line 6121
PHP Null Coalescing Operator - PHPTechTutor 2 - 2 PHPTechTutor PHP and Laravel Blog July 5, 2025 Vivek Pawar PHP Null Coalescing Operator PHP Null Coalescing Operator

PHP Null Coalescing Operator

It has been 7 years since to release of PHP 7. Here is the tremendous enhancement of the ternary operator called as null coalescing operator.

Traditional Ternary Operator with shorthand (?:)

condition ? operand1 : operand2

condition ? (subCondition ? subOperand1 : subOperand2) : operand2

condition ?: operand

The ternary operator is I think most frequently used operator in the programming world. Here are the multiple ways that we can use the ternary operator. A ternary operator is a kind of operator where we can check multiple conditions in a single line.

Also, we can use the shorthand of the ternary operator,
The shorthand always checks for the null, false, empty(), !isset(), is_null().

Here are some examples of the ternary shorthand.

Ternary Operator Shorthand

Null coalescing operator (??)

Here is an explanation from php.net

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.

So it is the default check for (!isset() and is_null()) condition. Here are some examples of Null Coalescing Operators.

Null Coalescing Operator

Combine the Ternary and Null Coalescing Operator

What happened if we combine both operators in single condition checks? Here are some sample examples.

Null Coalescing And Ternary Shorthand

Do check out this great enhancement. This might help you with quick multiple conditions check and let me know the feedback in the comment section.

Happy Coding 🙂

Leave a Comment

Your email address will not be published. Required fields are marked *