php - 类型声明前的问号(?)在php(?int)中意味着什么

标签 php php-7.2

<分区>

我在https://github.com/symfony/symfony/blob/master/src/Symfony/Component/Console/Output/Output.php中看到过这段代码第 40 行他们正在使用 ?int。

public function __construct(?int $verbosity = self::VERBOSITY_NORMAL, bool $decorated = false, OutputFormatterInterface $formatter = null)
    {
        $this->verbosity = null === $verbosity ? self::VERBOSITY_NORMAL : $verbosity;
        $this->formatter = $formatter ?: new OutputFormatter();
        $this->formatter->setDecorated($decorated);
    }

最佳答案

它称为可空类型

?int 定义为 intnull

Type declarations for parameters and return values can now be marked as nullable by prefixing the type name with a question mark. This signifies that as well as the specified type, NULL can be passed as an argument, or returned as a value, respectively.

示例:

function nullOrInt(?int $arg){
    var_dump($arg);
}

nullOrInt(100);
nullOrInt(null);

函数 nullOrInt 将接受 null 和 int。

引用:http://php.net/manual/en/migration71.new-features.php

关于php - 类型声明前的问号(?)在php(?int)中意味着什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49404191/

相关文章:

php - 清除文本框字段后再次加载选择框

php - 对 array_values 的误解

php-7.2 - 获取 : Fatal error while I am trying to use the declare(strict_types=1) on my PHP file

php - 使用 brew 在 MacOS 上安装 php72

php - 从多个表中检索记录

php - 如何使用 perl 在 php mysql 中从 linux 服务器收集数据?

php - 将专辑轨道列表存储在数据库中

javascript - Jquery post数据显示在url中

php - pear channel 更新后 SSL 被破坏 pear.php.net

php - 使用 NoRewindIterator 实现 foreach 的正确方法