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.

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

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.