A switch or an associative array is preferable to a sequence of ifs. It's more readable and potentially more performant. Numerous ifs is a minor smell
PHP Code:
$pages = array(
'home' => array('title' => 'Home', 'file' => 'index.html'),
'about' => array('title' => 'About Us'),
'contact' => array('title' => 'Contact Us'),
...
'invalid' => array('file' => '404.html')
);
function getFile($pages, $page) {
if (isset($pages[$page])) {
return $pages[$page];
} else {
return $page . '.html';
}
}
// prevent injection attacks
if (isset($pages[ $_REQUEST['page'] ])) {
$page = getFile($pages, $_REQUEST['page']);
$title = $pages[$page]['title'];
...
} else {
// invalid page requested
header('HTTP/1.0 404 Not Found');
$page = $pages['invalid']['file'];
}
include('header.php');
include($page);
include('footer.php');