Câu hỏi phỏng vấn PHP
Câu hỏi

What is use of Null Coalesce Operator?

Câu trả lời

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:

php Copy
$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:

php Copy
$array = ['key' => 'value'];
$value = ...
middle

middle

Gợi ý câu hỏi phỏng vấn

senior

What's better at freeing memory with PHP: unset() or $var = null ?

middle

Does PHP support method overloading?

middle

Maximum how many arguments are allowed in a function in PHP?

Bình luận

Chưa có bình luận nào

Chưa có bình luận nào