street9009 Posted October 26, 2007 Share Posted October 26, 2007 I never use the darn things because to me they are so hard to follow. But I have a line of code I need to discern and I'm hoping someone can help briefly. $aShipTo['COMPANY'] = (isset($aFieldValues['SCOMPANY']) && $aFieldValues['SCOMPANY'] != '') ? $aFieldValues['SCOMPANY'] : (isset($aFieldValues['COMPANY']) && $aFieldValues['COMPANY'] != '') ? $aFieldValues['COMPANY'] : $aFieldValues['SFCONTACT'] . ' ' . $aFieldValues['SLCONTACT']; Now, I'm assuming it gets converted like this: if (isset($aFieldValues['SCOMPANY']) && $aFieldValues['SCOMPANY'] != '') { $aShipTo['COMPANY'] = $aFieldValues['SCOMPANY'] } else if (isset($aFieldValues['COMPANY']) && $aFieldValues['COMPANY'] != '') { $aShipTo['COMPANY'] = $aFieldValues['COMPANY'] } else { $aShipTo['COMPANY'] = $aFieldValues['SFCONTACT'] . ' ' . $aFieldValues['SLCONTACT']; } Am I right in this assumption? Quote Link to comment https://forums.phpfreaks.com/topic/74916-solved-simple-question-for-someone-who-knows-ternary-operators-better-than-i/ Share on other sites More sharing options...
premiso Posted October 26, 2007 Share Posted October 26, 2007 Edit, removed due to mis-information. Quote Link to comment https://forums.phpfreaks.com/topic/74916-solved-simple-question-for-someone-who-knows-ternary-operators-better-than-i/#findComment-378782 Share on other sites More sharing options...
street9009 Posted October 26, 2007 Author Share Posted October 26, 2007 So how would the above convert to "conventional" if-else syntax? Quote Link to comment https://forums.phpfreaks.com/topic/74916-solved-simple-question-for-someone-who-knows-ternary-operators-better-than-i/#findComment-378786 Share on other sites More sharing options...
premiso Posted October 26, 2007 Share Posted October 26, 2007 if (isset($aFieldValues['SCOMPANY']) && $aFieldValues['SCOMPANY'] != '') { $aShipTo['COMPANY'] = $aFieldValues['SCOMPANY'] }elseif (isset($aFieldValues['COMPANY']) && $aFieldValues['COMPANY'] != '') { $aShipTo['COMPANY'] = $aFieldValues['COMPANY'] }else { $aShipTo['COMPANY'] = $aFieldValues['SFCONTACT'] . ' ' . $aFieldValues['SLCONTACT']; } Like you had it before. For more complex issues you really shouldn't use the ternary operator as you stated it can get confusing to others who look at your code. It was designed to handle small if's that did not need much logic to them. Quote Link to comment https://forums.phpfreaks.com/topic/74916-solved-simple-question-for-someone-who-knows-ternary-operators-better-than-i/#findComment-378790 Share on other sites More sharing options...
street9009 Posted October 26, 2007 Author Share Posted October 26, 2007 Like I said, I don't use them because they confuse the heck out of me. This one was written by someone else and I'm trying to work through it. Thanks for the confirmation that I did at least convert it right and for the info on using these. Quote Link to comment https://forums.phpfreaks.com/topic/74916-solved-simple-question-for-someone-who-knows-ternary-operators-better-than-i/#findComment-378792 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.