tags:

views:

147

answers:

4

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?

+3  A: 

You have a typo. This:

? $signup_errors['catcha'] :

Should be this:

? $signup_errors['captcha'] :

catcha -> captcha

Ionuț G. Stan
He's got two typos. Look closer.
Chris Lutz
Chris, that's the only one I could spot. And I tested the code in the CLI actually.
Ionuț G. Stan
+1  A: 

You have misspelled 'captcha' as 'catcha'.

too much php
+2  A: 

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']; }
Andrew Medico
+5  A: 

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

Greg Hewgill
The parentheses are OK. Actually they're pretty much useless in both cases.
Ionuț G. Stan