Includes and Site Modularization

The creators of PHP understood the need to modularize and re-use code. And by nature of being server-side, PHP is uniquely qualified to meet this need. PHP files can include other php and/or html files as they are being processed through the simple call of a function. This allows developers to re-use sections of HTML and PHP code, load libraries of PHP functions and include secure information into a page from a location on the server with restricted access to the rest of the world.

To accomplish this task, PHP provides four functions: include(), include_once(), require() and require_once(). Each serves its own purpose which we shall now discuss. We start with the big difference: include vs require. Both methods will include code at the point of the method call. In other words, the code in the called file replaces the method call at the location of the call. include("../../secure/dbConnection.php"); include("navigation.html");

The first major difference is how/when the files are replaced. Include calls are replaced when the code reaches that point in the script. This means that include methods can be called conditionally, and if the method is not called, the code will not load. Require methods, on the other hand, will load the content regardless of if the condition is true or not. Thus the inclusion of the code is "required." Additionally, include statements have the ability to be a function on their own and return a value from the call while require functions do not have this capability. $result=include("getSomething.php"); //getSomething.php has a statement in it similar to return value;

The next difference is between the original methods (include/require) and their restrictive counterparts (include_once/require_once). If multiple include/require calls are made in a page to the same library, the page will load said library repeatedly which can cause a problem as they loading multiple functions with the same names. This can cause major issues in a page as php, along with most languages, does not allow functions to have the same signature within a file. This can occur accidentally when included files include other files. For example, we may bring in several php scripts that each have an include call to the database connection. To avoid this, we would instead use include_once.

Variable Scope

Like most languages, PHP variables are only valid in the scope in which they are declared. Hence, a variable declared in a function is not valid outside that function and vice-versa. Free floating variables cannot be used in methods unless sent in as a parameter. Unless otherwise defined, all variables in PHP are local and follow the basic scope rules.

A global variable overrides the scope boundaries and can be used across scopes. Global variables can be declared in general code or in a method AND are then accessible across the entire file. This allows the creation of a variable in a condition or method that can then be used throughout the rest of the program. Global variables are declared as follows: global $x;
A step above global variables are superglobal variables. These are variables that are built into php that are available in all scopes. Most of the superglobal variables operate as associative arrays. Here is a list of some of the superglobal variable arrays and their uses:

$_GET

HTML forms that send information by method=GET attach the required information to the url of the action variable in name/value pairs. So, an input tag with name attribute='first' that a user completes with "Bob", gets sent as the pair first=Bob. This pair gets appended to the target url as follows: http://www.site.com/processForm.php?first=Bob. The receiving page (processForm.php) can then receive the information using the $_GET array with the name as the index of the associative array. $firstName=$_GET['first']; //$firstName now stored "Bob" This concept of variables appended to the url can be utilized WITHOUT the need for a form to send the information. We can simply append information onto a url and send it from page to page as desired. <a href="welcome.php?first=Bob&location=NewYork">Welcome</a> On welcome.php, we can now access that information using the $_GET superglobal array.

The Sessions Array

One way to maintain information across php pages is to send it as hidden inputs of a form. This can work within a mutliple page form submission, but is not always practical when we need to simply maintiain a username or access level as a user travels throughout a site. In this case, we can use the superglobal SESSION array. The SESSION array is a set of variables that are valid across all pages during a users visit to a site. Sessions are like cookies in that they store information about the user, except they do not reside on the computer. This has its benefits and drawbacks. Benefits include that the user does not have access to the information stored (as it is on the server). Drawbacks include that they information is erased when the session is ended.

For SESSION variables to work on a page, the page must start a session. The session start method must be the first thing on the page - even before any HTML code. The session start method looks for a user key, if it finds one, it continues it. Otherwise, it begins a new one. This means that if you log into two different sites in the same browser that both use sessions, the variables may become confused and one session will end. There is a way to specify a session, using session_id, but that is out of the scope of this lesson. session_start();
Session variables can be set, accessed or altered using the standard array notation. The SESSION array is an associative array, so the indicies can be strings. An example is as follows:> $_SESSION['username']=$_GET['username']; $_SESSION['userlevel'] = 5; $_SESSION['email'] = "user@company.com";
Finally, when a user is set to log out of a web page, and/or you want to eliminate all of the session variables and values, use the destroy method. This will terminate a users session. // remove all session variables session_unset(); // destroy the session session_destroy()