What do two question marks mean in php

藏色散人
Release: 2023-03-14 18:14:01
Original
10237 people have browsed it

The two question marks in php are a new NULL merging operator ?? introduced in php7. Its use method is such as "$username = $_GET['user'] ?? 'nobody';".

What do two question marks mean in php

#The operating environment of this article: Windows 7 system, PHP version 7.1, Dell G3 computer.

What do the two question marks in php mean?

In fact, the two question marks?? are new expressions introduced by php7:

In the PHP7 version, there is an additional NULL merge operator??, the example is as follows:

Example

<?php
// 如果 $_GET[&#39;user&#39;] 不存在返回 &#39;nobody&#39;,否则返回 $_GET[&#39;user&#39;] 的值
$username = $_GET[&#39;user&#39;] ?? &#39;nobody&#39;;
// 类似的三元运算符
$username = isset($_GET[&#39;user&#39;]) ? $_GET[&#39;user&#39;] : &#39;nobody&#39;;
?>
Copy after login

php7 used to often use ternary operation expressions:

Ternary operator:

Another conditional operator is " ?:" (or ternary) operator.

Syntax format

(expr1) ? (expr2) : (expr3)
Copy after login

The value when expr1 evaluates to TRUE is expr2, and when expr1 evaluates to FALSE, the value is expr3.

Since PHP 5.3, the middle part of the ternary operator can be omitted. The expression expr1 ?: expr3 returns expr1 if expr1 evaluates to TRUE and expr3 otherwise.

Recommended learning: "PHP Video Tutorial"

The above is the detailed content of What do two question marks mean in php. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
php
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!