Jump to content

[SOLVED] Simple question for someone who knows ternary operators better than I


Recommended Posts

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?

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.

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.