Jeff Voss
Jeff Voss

Reputation: 3695

A Better way to write a Switch in PHP?

I use this example below to ask how to pass an array into a Switch instead of listing them all out like this?

Perhaps if I had an array being fetched from a database for all the 'pages' as page.

I figured this method wasn't the most elegant approach.

switch($_GET['page'])
{
case 'home':
    include('home.php');
    break;
case 'oem-customers':
    include('oem-customers.php');
    break;
case 'job-shop': 
    include('job-shop.php');        
    break;
case 'anodized-magnet-coils':
    include('anodized-magnet-coils.php');
    break;
case 'design':
    include('design.php');
    break;
case 'services':
    include('services.php');
    break;
case 'black-foil':
    include('black-foil.php');
    break;  
case 'contact':
    include('contact.php');
    break;
case 'order';
    include('order.php');
    break;
default: 
    include('home.php');
}

Upvotes: 0

Views: 2189

Answers (7)

Gianpaolo Di Nino
Gianpaolo Di Nino

Reputation: 1147

I would suggest a total different way:

$base_path = '/path/to/dir/';
$req = basename($_GET['page'] . '.php');
$page = $base_path . $req;
$file = file_exists($page) ? $page : $base_path . 'home.php'; 
include($file);

Upvotes: 2

Pebbl
Pebbl

Reputation: 35995

If it were me I'd move your scripts that can be requested into a requests/ folder and do the following:

$script = basename( $_GET['page'] );
$dir = 'requests/';
$file = $dir . $script . '.php';
if ( !file_exists( $file ) ) {
  $file = $dir . 'home.php';
}
include( $file );
  1. Much more compact than anything else.
  2. Will auto update whenever you add a new page.
  3. Built-in check for existence of file before include.
  4. Safe due to the containing requests/ folder and basename().

Upvotes: 2

darma
darma

Reputation: 4747

Example with an array :

$pages = array(
    'oem-customers',
    'job-shop',
    'anodized-magnet-coils',
    'design',
    'services',
    'black-foil',
    'contact',
    'order' 
);

if(in_array($_GET['page'], $pages)){
    include($_GET['page'].'.php');
}else{
    include('home.php');
}

Upvotes: 3

Igor Serebryany
Igor Serebryany

Reputation: 3331

well, you seem to have a pretty good match between $_GET['page'] and the filename of the page; an easy thing to do is:

   $toInclude = "{$_GET['page']}.php";
   include($toInclude);

of course, you want to be careful; someone could trick you by passing something bad in page, like '../../some_other_project/delete_all_data.php'. so can keep a validation list of all of the pages you allow:

   $validPages = array('order.php', 'home.php');
   if (!in_array($toInclude, $validPages))
       $toInclude = 'home.php';

Upvotes: 2

Karoly Horvath
Karoly Horvath

Reputation: 96258

You can create a mapping:

$mapping = array(
   'home' => 'home',
   'oem-customers' => 'oem-customers',
   #....
);

$page = $_GET['page'];
$file = isset($mapping[$page]) ? $mapping[$page] : 'home';
include($file.'.php');

Or first look up the file, check if the file exists, and perhaps use extra mapping if needed. Note: Sanitize the input, users could send ../../etc/whatever in the request.

Upvotes: 3

JCOC611
JCOC611

Reputation: 19709

You could do the following:

switch($_GET['page']){
  case 'home':case 'oem-customers':case 'job-shop':case 'anodized-magnet-coils':
  case 'black-foil':case 'services':case 'design':case 'contact':case 'order':
    include($_GET['page'].'.php');
    break;
  default: 
    include('home.php');
}

Upvotes: 2

Musa
Musa

Reputation: 97672

How about

switch($_GET['page'])
{
case 'home':
case 'oem-customers':
case 'job-shop': 
case 'anodized-magnet-coils':
case 'design':
case 'services':
case 'black-foil':
case 'contact':
case 'order';
    include("$_GET[page].php");
    break;
default: 
    include('home.php');
}

Upvotes: 5

Related Questions