simpli Posted April 5, 2009 Share Posted April 5, 2009 Hi, It's my first crack at a ternary operator. It's nested in an echo and it's not working. Can anyone troubleshoot it and tell me why? Some context: $error is an array containing possible error msg (this is in a validation form). So if the form was invalid, I want to apply the error class css that will format the field a certain way. Can you help troubleshoot? I'm stuck. J-R function choosePlayers($error) { //Choix des dix joueurs global $nombre_joueurs; $nbjoueurs = $nombre_joueurs; echo '</br>'; echo '<fieldset>' . "\n"; echo '<ol>' . "\n"; for ($i=1; $i <= $nbjoueurs; $i++) { echo '<li><input name="player_' . $i . '" id="player_' .$i . isset($error["player_" . $i]) ? 'class="error"' :'' . '" type="text" size="30"></li></br>' . "\n"; //echo isset($error["player_" . $i]) ? 'class="error"' :''; } echo '</ol>'; echo '</fieldset>'; } Quote Link to comment https://forums.phpfreaks.com/topic/152645-why-is-this-ternary-operator-not-working/ Share on other sites More sharing options...
Yesideez Posted April 5, 2009 Share Posted April 5, 2009 You're inside a function and I can't see any mention of defining $error as a global or seeing it given a value. Quote Link to comment https://forums.phpfreaks.com/topic/152645-why-is-this-ternary-operator-not-working/#findComment-801645 Share on other sites More sharing options...
simpli Posted April 5, 2009 Author Share Posted April 5, 2009 $error is defined outside of the function and I pass it as a parameter to this function. I checked it and it's defined. It's that last bit that isn't working. JR Quote Link to comment https://forums.phpfreaks.com/topic/152645-why-is-this-ternary-operator-not-working/#findComment-801655 Share on other sites More sharing options...
Daniel0 Posted April 5, 2009 Share Posted April 5, 2009 Put it in parentheses. Quote Link to comment https://forums.phpfreaks.com/topic/152645-why-is-this-ternary-operator-not-working/#findComment-801657 Share on other sites More sharing options...
Yesideez Posted April 5, 2009 Share Posted April 5, 2009 My bad - didn't see it coming in with the function as a parameter didn't notice the scroll bar. (isset($error["player_" . $i]) ? 'class="error"' :'' . '" type="text" size="30"></li></br>') Quote Link to comment https://forums.phpfreaks.com/topic/152645-why-is-this-ternary-operator-not-working/#findComment-801663 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.