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

middle

When should I use require_once vs require ?

senior

Explain the Order of Precedence for Traits in PHP

junior

What are the differences between die() and exit() functions in PHP?

Bình luận

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

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