[英]PHP ternary operator not working
The code below takes an array value, if it's key exist it should echo out it's value, the ternary if/else part works but the value is not showing up, can anyone figure out why it won't? 下面的代码接受一个数组值,如果存在键,则应回显它的值,三元if / else部分起作用,但该值未显示,有人能弄清楚为什么不这样做吗?
$signup_errors['captcha'] = 'error-class';
echo(array_key_exists('captcha', $signup_errors)) ? $signup_errors['catcha'] : 'false';
Also where I have it echo out false, I do not need an output if a key does not exist, should I just delete the word false or is there something else to make the code only show 1 value? 另外,在我回声为false的地方,如果键不存在,则不需要输出,我应该只删除单词false还是要使代码仅显示1个值?
I think you've got a parenthesis in the wrong place: 我认为您在错误的位置加上了括号:
echo(array_key_exists('captcha', $signup_errors) ? $signup_errors['captcha'] : 'false');
Also, check your spelling of 'captcha'
. 另外,检查您的
'captcha'
拼写。
You have a typo. 你有错字 This:
这个:
? $signup_errors['catcha'] :
Should be this: 应该是这样的:
? $signup_errors['captcha'] :
catcha -> captcha catcha->验证码
I think you meant: 我想你的意思是:
echo(array_key_exists('captcha', $signup_errors) ? $signup_errors['captcha'] : 'false');
Or if you want no output when the key doesn't exist, use an 'if' statement, not the ternary operator: 或者,如果在键不存在时不希望输出,请使用'if'语句,而不是三元运算符:
if (array_key_exists('captcha', $signup_errors)) { echo $signup_errors['captcha']; }
您已将'captcha'拼写错误为'catcha'。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.