What is use of Null Coalesce Operator?
What is use of Null Coalesce Operator?
The Null Coalesce Operator (??) is a PHP language construct introduced in PHP 7.0. It provides a concise way to check if a variable is set and not null, and if not, return a default value. Here's how it works:
$result = $variable ?? 'default value';
In this example, if $variable
is set and not null, $result
will be assigned the value of $variable
. If $variable
is null, $result
will be assigned the string 'default value'.
The Null Coalesce Operator is particularly useful when working with arrays and variables that may not be set or may contain null values. It helps to avoid the need for lengthy conditional statements and can make your code more readable and concise.
Here's an example of how you can use the Null Coalesce Operator with arrays:
$array = ['key' => 'value'];
$value = ...
middle
Gợi ý câu hỏi phỏng vấn
Chưa có bình luận nào