0

Hi,

I have a small question. I am developing a php code and it has nested if else statements and it has like 7-8 nested if and else statements. I am looking to reduce the nested if else to reduce the complexity of understanding the code. Is there a better way to reduce the code. Here is the sample example

if() {
 if() {
  if() {
   if() {
   } else {
   }
  } else {
  }
 } else {
 }
} else {
}

Something like this and the structure is very huge and all the if's are dependent on the if's above it.

3
Contributors
3
Replies
4
Views
7 Years
Discussion Span
Last Post by chrishea
0

Have you looked at using a "CASE" statement?
switch ($roll){
case 1:
$romValue = “I”;
break;
case 2:
$romValue = “II”;
break;
case 3:
$romValue = “III”;
break;
case 4:
$romValue = “IV”;
break;
case 5:
$romValue = “V”;
break;
case 6:
$romValue = “VI”;
break;
default:
print “This is an illegal die!”;
} // end switch

0

Yes, I was thinking of switch statement and just wondering how to write the else statement in a switch. Is it efficient to use switch instead of nested if and else statements.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.