Using our SwitchBox is even easier as it only takes two lines of code to implement. Let’s look at the code of index.php, and then I will explain it.
<?php
// Set switch
$switch = "index";
// Call SwitchBox
require './includes/SwitchBox.php';
/* Follow with index.php's code */
?>
What’s happening is simple. We set our switch (used by the SwitchBox so that the SwitchBox knows what to do), and then we call the SwitchBox. Depending on what
$switch
was, our SwitchBox takes the appropriate actions. In this case, our SwitchBox included the files ‘fns_database.php’ and ‘fns_index.php’. After those two lines of code, the global variables in the SwitchBox, everything in fns_database.php, and everything in fns_index.php, are all available right on index.php. If you did not want to include any files for the page, you could simply leave the line:
$switch = “…”;
out of the page. Then only the global variables would be available in the application.
Easy right? Ok so let’s review…