Passing Variables – PHP Include and Require

Frequently when you write a php application you have a task or functionality in your code that you will use over and over again.  For example, when verifying form inputs, you would want to make sure that a number is an integer, and eliminate all letters and decimal points.  This is handled by creating an integer verification function, you might call checkint().  You usually place the checkint() function in another library file that contains all your other verification functions, maybe called verification.php.

The normal flow of your application runs something like this.  You get your form information from your html web page and with a "post" method, feed it back to your php controller file.  If there is an integer value in the form, you assign it to a variable, "$intvalue" for example, you want to verify the data coming back from the form is what it's meant to be and is not corrupted, so you call your "checkint($intvalue)" function to check the "$intvalue" variable.

There's only one problem, since the Internet is stateless, the controller file knows nothing about any other file, function, or variable in the system.  The controller file does not have a clue as to what "checkint($intvalue)" function means, or where it is located.  And it responds with an "undefined function" error message.

The above situation calls for an "include" or "require" function in your controller file.  We want to "include" or "require" the verification.php file, that has the checkint() function in it, to be placed in the controller script.  And that is what the "include" and "require" functions do.  They open the verification.php file, copy, and place its contents in the controller file.  After this operation is completed, the checkint() function is now in the controller file, and you can go ahead and use the checkint function to verify your data.

Here's the syntax for ways you can include another file in your current file:

/* With includes, all of these are valid */
include " ../files/verification.php";
include ("C:\wamp\www\mysite\files\verification.php");
include "";
include_once ("verification.php");

/* You can substitute require for include in all the above examples.  */

require ("verification.php");
require_once "verification.php";

You can use () or not, since most functions use (), it is normal to use the ()'s with your includes and requires.

You'll notice in line 4, I returned the variables "clr" and "style" with the include file which could have come from another server. You can retrieve the color and style variable with a call to the php get, like so: $scrclr = $_GET['color']; and $varstyle = $_GET['style'];

Include and require do the same thing, the difference is if you can't find the file with require, it will shut your application down, while with include it will throw an error message and continue with the application.  Other than that include and require are the same, and can be used interchangeably.

You can use include and require in conditional statements to only bring the file in if a certain condition is met, like so.

if ( $layer = 12 ){
include ("../file/layer12.php");

/* the {} are required with an include or require  */

A require will bring in the file even if it is a false condition, while an include will not.

Why use include_once or require_once?  Your not allowed to declare the same variable or function twice within a script, or php will throw an error.   The error message will read something like this:

"Fatal error: Cannot redeclare checkint() (previously declared in :67) in c:\www\wamp\mysite\files\verificaiton.php on line 71"

This error can also come when an included file calls another include, and this is where problems can start that will drive you nuts.

If you use "include_once" or "require_once,"  php will check to see if the file has already been included, if it has it will proceed without including the file, which will prevent the above error.

Another error with includes is improper order.  You call the function you need before you include the file.  PHP runs scripts in order, the include needs to come first.

In a complex application with many files, some of my most frustrating errors have come with includes and trying to figure out why I'm getting the redeclare error message.  I've also had problems with include_once, and the undeclared variable error message.

Although the error message may tell you where the problem is, it doesn't tell you where the original include occurred or why the function has not been declared already, which sometimes causes you to track through your system.  A patch, if you are really hung up by an error message, and are short on time, is to ditch the include and to redundantly repeat the function in the script itself, not what I call eloquent coding, but it works.


Passing Variables – PHP Include and Require — 2 Comments