Jump to content

Recommended Posts

I need help condensing my conditionals. I have lots of elseifs, and I want to take them out. Should I put it into a single elseif, or should they remain seperate for the sake of maintainability?

 

How would I put them into a single if/else?

 

if (($i == 7) && ($j ==  && ($containsLimit != FALSE))
		{
			echo '</tr></table><br /><br /><br />';
			$i = 1;
			$j = 1;
			$k = 1;
		}
		//If that was a limitless train, and that was the last possible row, end the table.
		elseif (($containsLimit == FALSE) && ($k == $thisLimit))
		{
			echo '</tr></table><br /><br /><br />';
			$i = 1;
			$j = 1;
			$k = 1;
		}
		//If that was a limited train, and that was the last possible row, end the table.
		elseif (($containsLimit == TRUE) && ($k == $rowsLeft))
		{
			echo '</tr></table><br /><br /><br />';
			$i = 1;
			$j = 1;
			$k = 1;
		}

		//If that was the sixth column, and not the eighth row, end the row, and start a new one.
		elseif (($i == 7) && ($j != )
		{
			echo '</tr><tr>';
			$i = 1;
			$j++;
			$k++;
		}

		else
		{
			$i++;
			$k++;
		}

Link to comment
https://forums.phpfreaks.com/topic/169718-condensing-ifelse/
Share on other sites

A switch is only useful when you have a single parameter as the switch. I have a feeling there's a more logical way to approach your problem, but I can't see what you are actually doing.

 

The first three conditions all have the same result. So there is no reason you have to separate them. This should accomplish the same thing

 

//If that was a limitless train, and that was the last possible row, end the table.
//OR If that was a limited train, and that was the last possible row, end the table.
if ( (($i == 7) && ($j ==  && ($containsLimit != FALSE))
     || (!$containsLimit && $k==$thisLimit)
     || ($containsLimit && $k==$rowsLeft)
   )
{
echo '</tr></table><br /><br /><br />';
$i = 1;
$j = 1;
$k = 1;
}

//If that was the sixth column, and not the eighth row, end the row, and start a new one.
elseif (($i == 7) && ($j != )
{
echo '</tr><tr>';
$i = 1;
$j++;
$k++;
}
else
{
$i++;
$k++;
}

Link to comment
https://forums.phpfreaks.com/topic/169718-condensing-ifelse/#findComment-895394
Share on other sites

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.