Table of Contents
Test code
Conclusion
Home Backend Development PHP7 What is the difference between '??' and '?:' introduced in PHP7

What is the difference between '??' and '?:' introduced in PHP7

May 11, 2021 am 09:24 AM

This article will introduce to you the difference between "??" and "?:" introduced in PHP7. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to everyone.

What is the difference between '??' and '?:' introduced in PHP7

Test code

Input test:

<?php
    $array = [
        &#39;a&#39; => 1,
        &#39;b&#39; => 2,
        &#39;c&#39; => [],
    ];

    $a = $array[&#39;c&#39;] ?? 0;
    $b = $array[&#39;c&#39;] ?: 0;
    $c = $array[&#39;d&#39;] ?? 0;
    $d = $array[&#39;d&#39;] ?: 0;
    $e = $array[&#39;c&#39;] ? 1 : 0;
    $f = isset($array[&#39;c&#39;]) ? 1 : 0;
    $g = $array[&#39;d&#39;] ? 1 : 0;
    $h = isset($array[&#39;d&#39;][&#39;e&#39;]) ? 1 : 0;
    $i = !empty($array[&#39;c&#39;]) ? 1 : 0;
    $j = !empty($array[&#39;d&#39;]) ? 1 : 0;

    var_dump($a);
    var_dump($b);
    var_dump($c);
    var_dump($d);
    var_dump($e);
    var_dump($f);
    var_dump($g);
    var_dump($h);
    var_dump($i);
    var_dump($j);
Copy after login

Output result:

PHP Notice:  Undefined index: d in /home/fanyu/abc.php on line 11
PHP Notice:  Undefined index: d in /home/fanyu/abc.php on line 14
array(0) {
}
int(0)
int(0)
int(0)
int(0)
int(1)
int(0)
int(0)
int(0)
int(0)
Copy after login

Conclusion

  1. $a ?? 0 is equivalent to isset($a) ? $a : 0.

  2. $a ?: 0 is equivalent to $a ? $a : 0.

  3. empty: Determine whether a variable is empty (null, false, 00, 0, '0', ‖ and the like will all return true).

  4. isset: Determine whether a variable is set (values ​​such as false, 00, 0, '0', ‖ will also return true).

Recommended learning: php video tutorial

The above is the detailed content of What is the difference between '??' and '?:' introduced in PHP7. For more information, please follow other related articles on the PHP Chinese website!

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)