Posted by: prajapatinilesh | October 29, 2008

PHP Include and Require Functions

With PHP, there are two ways to include a file. There is the include() statement and the require() statement. Both constructs are very similar except on how they handle failures. Include() produces a Warning while require() results in a Fatal Error.
I prefer to not to use the require() statement, because if there’s an error, the rest of the page will not be displayed. Include() does not behave this way, the script will continue regardless and all of the other code will be executed.
When a file is included, any variables available at that line in the calling file will be available within the called file, from that point forward. For example, you may want to store all of your php functions or database username and passwords into a file called “inc_funct.php” and have all the other pages include it. This will save you the headache of having to retype the code over and over again.

Source PHP Code:

// include a file.
// display the value of the included file.
echo $password;

Source PHP Code of Included File (inc_info.php):

$password = “1234”;


Just be sure to properly type the path to your file. Note, if you’re including files that are in higher directories (parent directories), it’s best to type out the full path name starting from the home directory (ex: home/user/public/file.php).


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s


%d bloggers like this: