eldan88 Posted May 15, 2013 Share Posted May 15, 2013 Hey! I was having trouble using the ternary operator to check if an array exists in a table. For example if $name['name'] exists echo the value otherwise echo the "None". Any help would be appreciated! Thanks Quote Link to comment https://forums.phpfreaks.com/topic/278028-question-about-using-the-ternary-operator/ Share on other sites More sharing options...
Irate Posted May 15, 2013 Share Posted May 15, 2013 Basically, the ternary operator is like an if ... else ... statement. $array['key'] ? echo "Array key exists." : echo "Array key does not exist."; could be written as if($array['key'){ echo "Array key exists."; } else { echo "Array key does not exist."; } Quote Link to comment https://forums.phpfreaks.com/topic/278028-question-about-using-the-ternary-operator/#findComment-1430198 Share on other sites More sharing options...
Strider64 Posted May 15, 2013 Share Posted May 15, 2013 <?php echo (isset($array['key'])) ? "Array key exists." : "Array key does not exist."; Quote Link to comment https://forums.phpfreaks.com/topic/278028-question-about-using-the-ternary-operator/#findComment-1430200 Share on other sites More sharing options...
eldan88 Posted May 15, 2013 Author Share Posted May 15, 2013 Basically, the ternary operator is like an if ... else ... statement. $array['key'] ? echo "Array key exists." : echo "Array key does not exist.";could be written as if($array['key'){ echo "Array key exists."; } else { echo "Array key does not exist."; } Thank you for your help but i think it should be written like this echo $array['key'] ? "Array key exists." : "Array key does not exist."; Quote Link to comment https://forums.phpfreaks.com/topic/278028-question-about-using-the-ternary-operator/#findComment-1430201 Share on other sites More sharing options...
Solution eldan88 Posted May 15, 2013 Author Solution Share Posted May 15, 2013 <?php echo (isset($array['key'])) ? "Array key exists." : "Array key does not exist."; That Can also work. Thanks Quote Link to comment https://forums.phpfreaks.com/topic/278028-question-about-using-the-ternary-operator/#findComment-1430203 Share on other sites More sharing options...
Irate Posted May 15, 2013 Share Posted May 15, 2013 Both options are possible as there is never only one solution to a problem. But I'm glad that you could be helped here. Quote Link to comment https://forums.phpfreaks.com/topic/278028-question-about-using-the-ternary-operator/#findComment-1430205 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.