简体   繁体   中英

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?

$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?

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' .

You have a typo. This:

? $signup_errors['catcha'] :

Should be this:

? $signup_errors['captcha'] :

catcha -> captcha

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 (array_key_exists('captcha', $signup_errors)) { echo $signup_errors['captcha']; }

您已将'captcha'拼写错误为'catcha'。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM