"123"); isset($test["hi"]) ? unset($-6ren">
gpt4 book ai didi

PHP If 速记

转载 作者:行者123 更新时间:2023-12-05 08:24:27 26 4
gpt4 key购买 nike

我想要一个简单的 if 速记来检查一个数组是否有一个特定的键,如果有则取消设置它。

$test = array("hi" => "123");
isset($test["hi"]) ? unset($test["hi"]);

为什么会出现解析错误?什么是正确的语法。

最佳答案

您不能在 ternary conditional operator 中使用 unset()因为该语言构造不返回值,并且运算符期望评估某个值,而不是执行代码块。另外,您的三元运算符不完整(缺少 : 组件)。

if 语句怎么会这么长?

if (isset($test["hi"])) unset($test["hi"]);

关于PHP If 速记,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6411075/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com