Search code examples
phpternary

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?


Solution

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