This is a collection of questions that come up every now and then about syntax in PHP. This is also a Community Wiki, so everyone is invited to participate in maintaining this list.
It used to be hard to find questions about operators and other syntax tokens.¹
The main idea is to have links to existing questions on Stack Overflow, so it's easier for us to reference them, not to copy over content from …
有人可以解释PHP中三元运算符速记(?:)和空合并运算符(??)之间的差异吗?
他们什么时候表现不同,以同样的方式(如果这种情况发生)?
$a ?: $b
Run Code Online (Sandbox Code Playgroud)
VS.
$a ?? $b
Run Code Online (Sandbox Code Playgroud) 我正在深入研究Symfony framwork(版本4)代码并发现代码的和平:
$env = $_SERVER['APP_ENV'] ?? 'dev';
Run Code Online (Sandbox Code Playgroud)
我不太确定这实际上是做什么但我想它扩展到类似的东西:
$env = $_SERVER['APP_ENV'] != null ? $_SERVER['APP_ENV'] : 'dev';
Run Code Online (Sandbox Code Playgroud)
或者可能:
$env = isset($_SERVER['APP_ENV']) ? $_SERVER['APP_ENV'] : 'dev';
Run Code Online (Sandbox Code Playgroud)
有人对这个问题有任何精确性吗?
编辑:
对于回答问题的所有人:谢谢所有标记我的问题的人都是否定的,因为已经存在类似的问题(PHP三元运算符vs null合并运算符):
确实,这两个问题非常相似.然而,每个人都很难想象"??" 被称为合并运算符.
否则我可以在官方文档上轻松找到它:
http://php.net/manual/en/migration70.new-features.php#migration70.new-features.null-coalesce-op
但是,对于那些不知道在php 7中添加了此功能的人来说,更有可能输入:
"php ?? operator"或"php double question mark operator"
这就是为什么我的问题具有附加价值的原因.
我请你,重新考虑你的负面反馈.谢谢
此致,Epixilog