为什么unset()在PHP三元运算符中不起作用 [英] Why unset() doesn't work in PHP ternary operator

查看:148
本文介绍了为什么unset()在PHP三元运算符中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以这是有问题的,但我对此视而不见。
即使阅读了两次文档( PHP比较运算符

So there is a problem with this, but i'm blind to it. Even after reading the documentation twice (PHP Comparison Operators)

isset($items['blog']) ? unset($items['blog']) : NULL;




解析错误:语法错误,意外的T_UNSET

Parse error: syntax error, unexpected T_UNSET


推荐答案

@Bryan指出,三元运算符中没有对语言构造的 function 调用。由于这里根本不涉及返回值,因此,只需执行以下操作:

A @Bryan points out, no function calls to language constructs within the ternary operator. Since there's no return value involved at all here though, just do:

unset($items['blog']);

无需事先检查该值是否已设置。如果不是这样,取消设置根本不会做任何事情。

There's no need to check if the value is set or not beforehand. If it is not, unset simply won't do anything.

这篇关于为什么unset()在PHP三元运算符中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆