0

I have a lot of php file.

To access each php I am using parameter.

Example : index.php?route=changepassword, index.php?route=aboutus, etc

I have a route.php to link the parameter to the correct file

Its better to use

If-Else statement

$route = $_GET['route'];
if ($route==changepassword) {
   //statement
} else if ($route==aboutus) {
   //statement
}

of using switch-case ?

$route = $_GET['route'];
switch ($route) {
case "changepassword" :
   //statement
   break;
case "aboutus" :
   //statement
   break;
}

This is just 2 file, I have 10+ file, it is better to use what?

Sieryuu
  • 1,510
  • 2
  • 16
  • 41

2 Answers2

4

You should use an array-map wherever you can:

$map = array(
    "changepassword" => "includes/changepw.php",
    "aboutus" => "templ/aboutus.php",
);

In that simple case (you didn't elaborate on your //statements) you can just utilize them as:

incude( $map[$_GET["route"]] );

As commonly you can map them onto classes or callbacks, or anonymous functions.

But lastly a map is more concise and easier to maintain.

mario
  • 144,265
  • 20
  • 237
  • 291
1

For these kind of conditions, go for code clarity before performance, if there's any performance to be gained with either solution.

I would definitely go with switch-case.

See this other thread with similar answers.

Community
  • 1
  • 1
emartel
  • 7,712
  • 1
  • 30
  • 58