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

junior

What's the difference between isset() and array_key_exists() ?

junior

What are PSRs? Choose 1 and briefly describe it

junior

What are the main differences between const vs define

Bình luận

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

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