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)
- PHP Include_once 500 error (4)
- include_once($_SERVER[DOCUMENT_ROOT] (4)
- include_once $_SERVER[DOCUMENT_ROOT] (3)
- php include_once tutorial (3)
- require_once tutorial (3)
- require within require php (2)
- require require_once include include_once php (2)
- php include include_once required (2)
- require in php sample program (2)
- include_once Internal Server Error (2)
- require_once 500 (2)
- include $_SERVER[DOCUMENT_ROOT] (2)
- include_once files use how to change url in php (2)
- php include_once connection file (2)
- php require include include_once (1)
- php include_once server error (1)
- php require 500 (1)
- php include require javascript (1)
- php inlcude_once javascript (1)
- PHP include() or include_once() (1)
- php include_once 500 internal server error (1)
- php include_once 500 server error (1)
- php include_once javascript (1)
- php include_once require (1)
- php include_once require_once (1)
- php require_once 500 Internal Server Error (1)
- using php to randomly include file (1)
- using include_once php (1)
- tutorial how to use include and required in defferent files in php validation (1)
- symfony The server returned a 500 Internal Server Error cpanel (1)
- spreadsheet_writer fgcolor (1)
- server error include_once php (1)
- require_once($_server[document_root] ’file php’); (1)
- require_once construct (1)
- require_once 500 – Internal server error (1)
- require php mit javascript (1)
- prototype <?=include_once( (1)
- programs using include and require in php (1)
- preg_match include require include_once require_once (1)
You will also be interested in ,
- ScriptAlias and Alias in httpd.conf at a glance
- Pass PHP Value To Javascript
- Find files inside a directory that starts with a specific string using php
- How to create random passwords using php
- Speed up wordpress using .htaccess part 1
- Simple XML Reading Using PHP
- Apache AddHandler application/x-httpd-php not working?
- Reading file using php
- Check whether url or domain exists using php
- Find any day before a given date using php
