jeudi 27 août 2015

Replace largo if, else if... else with FOR or something more compact

I receive via POST a value. Then, I´m comparing the value received (1, 2, 3, 4, 5) with variables pre defined in my code. Is it possible to do it with FOR or another way to simplify it without changing the functionality of the code? Yes, I need to receive the value as number and compare it with variables (no MYSQL).

I set on each test the name, eg: $varname = "Paul";

Here´s what I´m doing and what I´d like to change. Thanks a lot

// from previous page with input name thenumber
$thenumber = $_POST['thenumber'];

$option1 = "1";
$option1 = "2";
$option1 = "3";
$option1 = "4";
$option1 = "5";
$option1 = "6";
...
...
More options

if($thename == $option1)
{
$varname = "Paul";
}
else if ($thename == $option2)
{
$varname = "Louie";
}
else if ($thename == $option3)
{
$varname = "Dimitri";
}
...
...
...

Aucun commentaire:

Enregistrer un commentaire