首页 > 解决方案 > PHP 可空类型声明

问题描述

PHP 7.4 中的这个函数有什么区别吗?

public function foo(string $argumentThatCanBeNull = null) {}

public function bar(?string $argumentThatCanBeNull = null) {}

标签: phpfunctionmethodsparametersnullable

解决方案


没有区别,因为字符串默认可以为空。

顺便说一句,这是在 PHP 7.1 中引入的:https ://www.php.net/manual/en/migration71.new-features.php


推荐阅读