Jump to content

Condensing if/else


bundyxc

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

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.