I have a set of 13 questions, each in one large form. The questions are part of select fields, and I have huge blocks of "if, else if" statements to check them, like this:

if ($_POST[ques1] == "red") {
//actions for red
}
else if ($_POST[ques1] == "blue") {
//actions for blue
}
else if ($_POST[ques1] == "yellow") {
//actions for yellow
}
else if ($_POST[ques1] == "green") {
//actions for green
}
else if ($_POST[ques1] == "orange") {
//actions for orange
}
else if ($_POST[ques1] == "purple") {
//actions for purple
}

That's not the actual script, but that's just the idea of what I am trying to say. I am just wondering if there is an easier way to write that without having to do a ton of "Else if" statements?

    It's hard for me to suggest a "best" way without knowing what the actions will be in each case. If the actions tend to be significantly different, then a [man]switch[/man] construct might be easier to maintain. On the other hand, if the actions are similar but with a few minor details, I might build an array to hold the different values, then use the user-supplied value to control them:

    $data = array(
       'red' => array(
          'file'  => 'redpage.html', 
          'title' => 'The Red Title'
       ),
       'blue' => array(
          'file'  => 'bluepage.html',
          'title' => 'The Blue Title'
       )
       // etc. . . .
    );
    if(array_key_exists($_POST['color']))
    {
       echo "<h2>" . $data[$_POST['color']]['title'] . "</h2>\n";
       include $data[$_POST['color']]['file'];
    }
    else
    {
       // do a default action here or output an "invalid color" error message
    }
    
      Write a Reply...