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? 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. 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? 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. 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. 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
Archived
This topic is now archived and is closed to further replies.