So I’m wondering if the code (see below) is what could be called “good php code”. Its used where i work and i am trying to come up with a better solution to write this template.
The $requestedPage variable is used twice, first in the if and after that in the switch statement i don’t know why but i think there should be a better possibility.
The content is loaded through the require_once statement which will load the page content surrounded by the head.php and foot.php that contains the html – template.
<?php
header("Content-Type: text/html; charset=utf-8");
filter_var_array($_POST, FILTER_SANITIZE_STRING);
require_once 'lib/rb.php';
require_once 'head.php';
$requestedPage = $_POST['page'];
if(isset($requestedPage)) {
switch ($requestedPage) {
case 'list':
require_once 'page/list.php';
break;
default:
require_once 'page/home.php';
break;
}
} else {
require_once 'page/home.php';
}
require_once 'foot.php';
?>
You can remove the checking done twice. If the parameter is not set, it automatically goes into the default case of your switch statement.
So, your code can be
Edit: As @mario suggested in the comments, suppressing E_NOTICE which is shown when executing
$requestedPage = $_POST['page'];can prove problematic in future, when there is a relevant notice and you have suppressed it. So, you should either suppress notices ONLY in production code, or check if$_POST['page']is set before accessing its value, in the statement$requestedPage = $_POST['page'];