The Core PHP Constructs for Including Files
There are four core constructs for including files into your PHP scripts. The main objective is for you to create code in separate files and then be able to use that code to include functions, variables and etc, in other PHP scripts. You have two main options. To include() a file or to require() a file. We’ll get into the specifics in a moment and you’ll quickly understand what the differences are.
The include() Construct
The include() constrcut is the most commonly used method to include files amongst most developers. It’s purpose is to simply include a file and that’s it. If the file does not exist, it will return a warning and still allow the script that’s trying to include the file to continue to operate even if the warning is issued. Here’s a common example:
PHP Example:
<?php
include($_SERVER['DOCUMENT_ROOT'].’/get.php’);
?>
Now, all of the code, and functions from get.php will be available throughout the rest of the current PHP script for use with the rest of your code.
The include_once() Construct
Ok, the main difference between the include_once() construct and the include() construct is that if the file has already been included in this code execution, it will not be included again. This is a good method to use and I would recommend it above using the standard include() construct because it can prevent you from redeclaring functions that you may have already included previously. As your code becomes more complex, you may have files included in different files and when calling those files, you may start running into problems.
PHP Example:
<?php
include_once($_SERVER['DOCUMENT_ROOT'].’/get.php’);
?>
The require() Construct
The require() construct is the same as include, but one major difference. If the file does not exist, or cannot be included, a Fatal Error will be produced and the execution of the PHP script will be halted! This construct is important for those applications you may develop that have dependancies from other files which must be met in order for your script to function properly.
PHP Example:
<?php
require($_SERVER['DOCUMENT_ROOT'].’/get.php’);
?>
The require_once() Construct
This construct is the one that I use more than the other three. Personally, I feel that this construct takes into account all of the necessary reasons you would be including a file in the first place. Just like include_once() the require_once() construct determines if the file has already been included and if it has been, it will skip this instance. In addition, a Fatal Error will be produced just like the require() construct does if the file cannot be read or included.
PHP Example:
<?php
require_once($_SERVER['DOCUMENT_ROOT'].’/get.php’);
?>
Thanks Phphelper
Incoming search terms:
- include_once $_SERVER[\DOCUMENT_ROOT\] (4)
- include_once($_SERVER[DOCUMENT_ROOT] (4)
- PHP Include_once 500 error (4)
- include_once $_SERVER[DOCUMENT_ROOT] (3)
- require_once tutorial (3)
- php include_once tutorial (3)
- php include include_once required (2)
- require_once 500 (2)
- require require_once include include_once php (2)
- require in php sample program (2)
- include $_SERVER[DOCUMENT_ROOT] (2)
- include_once Internal Server Error (2)
- php include_once connection file (2)
- php using include (2)
- require within require php (2)
- php require_once 500 Internal Server Error (1)
- php require include include_once (1)
- php how to close require_once file (1)
- php tutorial include include_once (1)
- php include_once 500 internal server error (1)
- php require 500 (1)
- php inlcude_once javascript (1)
- PHP include() or include_once() (1)
- php include_once server error (1)
- php include_once javascript (1)
- php include_once 500 server error (1)
- programs using include and require in php (1)
- php use include or require (1)
- using || operator include file in php (1)
- using php to randomly include file (1)
- using include_once php (1)
- to increment the value of dropdown in pagination in php mysql (1)
- symfony2 blobtype (1)
- spreadsheet_writer fgcolor (1)
- server error include_once php (1)
- require_once($_server[document_root] file php); (1)
- require_once($_server[document_root] ’file php’); (1)
- require_once include_once internal server error (1)
- require_once construct (1)
- require_once 500 – Internal server error (1)
You will also be interested in ,
- Appending string using php to a text file
- Store Data In Remote DataBase Using cUrl or Execute a HTTP POST Using PHP CURL
- Display mysql table using for loop in php
- Finding size of a directory using php
- Find files inside a directory that starts with a specific string using php
- Simple file upload script using php
- Get textbox value from dropdown using ajax and php
- How to split a word or sentence delimited with slashes, commas or hyphens
- Using PHP_SELF best practices
- Post Data To Another Website Using cURL In PHP

