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:

$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

middle

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

junior

Differentiate between echo and print()

junior

What is the difference between var_dump() and print_r() ?

junior

What are the different scopes of variables?

Bình luận

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

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