Jump to content

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


street9009

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.