PHP Include Function

PHP Include( ) The PHP include() statement holds all the code/text that liesin the defined file and paste it into the file that uses the "include" statement. With the help of include statement,we can insert the code/text of one PHP file into another PHP file(before server executes it). Including file saves a lot of time or work. It means that we can create a standard header, footer, body, or menu file for all web pages. When we need to update a header, footer, or any web page, we can only update the included files. Syntax:
include 'filename'
Example1:
  • We have a PHP file called "php":
<?php
  echo '<a href="#">Home</a>
  <a href="#">AboutUs</a>
  <a href="#">Gallery</a>
  <a href="#">Testimonials</a>
  <a href="#">Favorites</a>
  <a href="#">Login</a>
  <a href="#">Contact</a>';
?>
  • To include the "php" in a page, we use include statement.
<!DOCTYPE html>
<html>
<body>
<h4>IncludeExample</h4>
<?phpinclude 'nav.php';?>
</body>
</html>
Output: IncludeExample
Home AboutUs Gallery Testimonials Favorites Login Contact
Example2:
  • We have a PHP file called "variable.php" with some defined variables:
<?php
$animal='Lion';
$flower='Lily';
$fruit='Strawberry';
?>
  • We include the PHP file "variable.php", in the calling file:
<!DOCTYPE html>
<html>
<body>
<h4>IncludeVariableExample</h4>
<?php include 'variable.php';
echo "$animal $flower $fruit."
?>
</body>
</html>

Difference between PHP include and require statements:

include require
1)  PHP include takes one argument, the filename that we want to include. PHP required() to perform the same process as include() does, takes one argument, the filename that we want to require.
2) Syntax: include 'filename'; example: include 'menu.php'; Syntax: require 'filename'; example:  require 'menu.php';
3)  PHP include() does not stop execution. PHP require() does stop execution once it gets a fatal error.
4) PHP include() gives the warning,and it continues to execute the script, in case if filename not found. If PHP require()found any problem like file not found or misnamed files and stops the execution once get a fatal error.
5) We can use PHP include() when the file is not required or not so important. When the script requires the file, it's better to use required().
Example3:
<!DOCTYPE html>
<html>
<body>
<h4>Included_File_Not_Exists</h4>
<?php include 'filenotexists.php';
echo "IncludeExample."
?>
</body>
</html>
Output:

Included_File_Not_Exists

Warning: include(filenotexists.php): failed to open stream: No such file or directory in C:\xampp\htdocs\inc.php on line 5 IncludeExample. In the above example, a file (filenotexists) is included with the include statement and PHP cannot find it, the PHP script will continue to execute. Example 4:
<!DOCTYPE html>
<html>
<body>
<h4>Require_File_Not_Exists</h4>
<?php require 'filenotexists.php';
echo "RequireExample."
?>
</body>
</html>
Output:

Require_File_Not_Exists

Fatal error: require(): Failed opening required 'filenotexists.php' (include_path='C:\xampp\php\PEAR') in C:\xampp\htdocs\req.php on line 5 In the above example, the echo statement will not be executed because the script execution dies after the require statement returned the fatal error.
  • PHP include_once:
The file will be included just once. This statement is similar to the include statement, except only one difference that if the file has been already included, it will not beincluded again, it returns TRUE.
  • PHP require_once:
The require_once statement is similar to require statement except PHP file has already been included and if so, not require it again.

Related Topics

PHP strcmp() Function

PHP strcmp() Function The strcmp() function in PHP compares two strings. It is a binary-safe case-sensitive string function. This function is similar to the strncmp() function, with the difference that you can specify the number of characters from each...

1 minute read.

PHP stristr() Function

PHP stristr() Function The stristr() function in PHP finds the first occurrence of a string and returns the matched substring. It is a case-insensitive function. Syntax stristr ( string $haystack , mixed $needle [, bool $before_needle ] ) Parameter haystack(required)- This parameter represents the input string. needle(required)- This parameter...

2 minutes read.

PHP array_diff_ukey() Function

The array_diff_ukey() function in PHP compares the keys from array1 against the keys from array2 and returns the difference containing all the entries from array1 that are not present in any of the other arrays. Syntax array_diff_ukey ( array $array1 , array $array2 [, array $... ], callable $key_compare_func ) Parameter array1(required)- This parameter signifies the...

1 minute read.

PHP array_splice() Function

PHP array_splice() Function The array_splice() function in PHP is used to remove a portion of the array and replaces them with the elements of the replacement array. Syntax array_splice (array &$input, int $offset [, int $length = count($input) [, mixed $replacement = array() ]] ) Parameter input(required)- This array represents the...

2 minutes read.

PHP array_key_last() function

The array_key_last () function in PHP gets the last key of an array if the specified array is not empty. Syntax array_key_last ( array $array )  Parameter array(required)- This parameter signifies the input array. Return This function returns the last key of the...

1 minute read.

PHP is_array() Function

The is_array() Function in PHP is used to  find whether a variable is an array or not. It returns a Boolean value true if the parameter var is an array else it returns...

1 minute read.

PHP is_null() Function

The is_null() function in PHP is used to  find whether the specified variable is a null or not. It returns a Boolean value true if the parameter var is null else it returns...

1 minute read.

PHP str_repeat() Function

PHP str_repeat() Function The str_repeat() function in PHP repeats a string a specified number of times. Syntax str_repeat ( string $input , int $multiplier ) Parameter string(required)- This parameter signifies the string to be repeated. multiplier(required)- This parameter represents the number of times the string will be...

1 minute read.

PHP str_word_count() Function

PHP str_word_count() Function The str_word_count() function in PHP counts the number of words inside the string and returns the information about words used in a string. String str_word_count( string $string [, int $format [, string $charlist ]] ) Parameter string(required)- This parameter represents the input string. format(required)- This parameterspecifies the...

2 minutes read.

PHP crypt() Function

PHP crypt() Function The crypt () function in PHP returns a hashed string using DES, Blowfish, or MD5 algorithms. Some constants of crypt() function are as follows: [CRYPT_STD_DES][CRYPT_EXT_DES][CRYPT_MD5][CRYPT_BLOWFISH][CRYPT_SHA_256][CRYPT_SHA_512] etc. Syntax crypt ( string $str [, string $salt ] )  Parameter str(required)- This parameter represents the string to be hashed salt(optional)- This parameter...

2 minutes read.

PHP For Loops

PHP for loop is used when the specified condition is predefined. Syntax: for (initialization; condition; increment/decrement ) { code to be executed; } Example <!DOCTYPE html> <html> <head> <title>PHP Tutorial</title> </head> <body> <?php echo " Print counting using for loop"."<br>"; for($i=1;$i<=5;$i++){ echo $i."<br>"; } ?> </body> </html> Output Print counting using...

1 minute read.

PHP implode() Function

PHP implode() Function The implode() function in PHP joins the specified array elements with a string. Syntax implode ( string $glue, array $pieces ) Parameter glue(optional)- It specifies what to put between the array elements. pieces(required)- This parameter represents...

1 minute read.

PHP bin2hex() Function | Convert Binary to Hexadecimal

PHP bin2hex() Function The bin2hex() function in PHP converts the specified string value of ASCII characters to hexadecimal value. Syntax bin2hex(str) Parameter str(Required): This parameter represents the string to be converted into hexadecimal. Return  This function returns...

1 minute read.

PHP array_uintersect_assoc() Function

PHP array_uintersect_assoc() Function The array_uintersect_uassoc() function in PHP calculates the intersection of arrays by comparing the data and indexes by separate callback functions. It returns an array containing all the values of the first input...

2 minutes read.

PHP end() function

PHP end() function The end() function in PHP sets the internal pointer of an array to its last element. This function returns the value of the last element Syntax end ( array &$array )   Parameter array (required)– This parameter represents the input array. Return This function returns...

1 minute read.

PHP if..Else..Elseif

PHP conditional statements are used to test the conditions. It supports different types of conditional statement. Following are the types of conditional statement if statement ..else statement ..elseif....else statement switch statement PHP if Statement PHP...

2 minutes read.

PHP ord() Function

PHP ord() Function The ord() function in PHP converts the first byte of a string to a value between 0 and 255. This function interprets the binary value of the first byte of string as an...

1 minute read.

PHP doubleval() Function

PHP doubleval() Function The doubleval() function in PHP is used to give the float value of a variable. This function is an alias of floatval(). Syntax doubleval ( mixed $var ) Parameter var(required)- This parameter represents the name of the variable that...

1 minute read.

PHP strtok() Function

PHP strtok() Function The strtok() function in PHP splits a string into smaller strings or tokenize the specified string.  Syntax strtok ( string $str , string $token ) Parameter str(required)- This parameter represents the input string to split into smaller strings or tokens. token(required)- It signifies...

1 minute read.

Composer Installation

Composer is an application-level package manager for the PHP Programming language. The development began in April 2011 and it was first released on March 1, 2012. Commands: require install update remove Installation:  Download the installer by visiting...

1 minute read.