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 sscanf() Function

PHP sscanf() Function The sscanf() function in PHP parses input from a string according to a given format. Syntax sscanf ( string $str , string $format [,mixed &$arg1,$agr2,$arg++... ] ) Parameter str(required)- This parameter specifies the string to read. format(required)- This parameter specifies the format to use. The conversion specification...

3 minutes read.

PHP array() function

PHP array() function The array() function in PHP is used to create an array. Syntax array ([ mixed $... ] ) Parameter …(optional)- This parameter represents the elements’ values along with keys of the array. The syntax of this is as follows: "index => values"...

1 minute read.

PHP vsprintf() Function

PHP vsprintf() Function The vsprintf() function in PHP returns a formatted string. This function accepts an array of arguments. Syntax vsprintf ( string $format , array $args ) Parameter format(required)- This parameter specifies the string and how to format the variables in it. The conversion specification of this parameter...

3 minutes read.

WordPress Theme

Writing our WordPress Theme is relatively simple. Requirements: Before we begin, we need to know about the following set of requirements. We need to have full knowledge of WordPress setup, which is either...

2 minutes read.

PHP array_pad() Function

PHP array_pad() Function The array_pad() function in PHP pads the array to the given length and value and returns a copy of the array padded to size and value specified by size and value parameter. The padding takes place as...

1 minute read.

PHP nl2br() Function

PHP nl2br() Function The nl2br() function in PHP inserts HTML line breaks before all newlines in a string. Syntax nl2br ( string $string [, bool $is_xhtml] ) Parameter sting(required)- This parameter specifies the input string. is_xhtml(optional)- This parameter states whether to use XHTML compatible line breaks...

1 minute read.

PHP Do While Loop

PHP do while loop is used to execute the code at least one time because condition is checked after executing the code. Syntax: do{ //code to be executed  }while(condition); Output <?php $n=1;  do{ echo "$n<br/>";  $n++;  }while($n<=5);  ?> Output 1 2 3 4 5 ← Prev Next →...

1 minute read.

PHP strlen() Function

PHP strlen() Function The strlen() function in PHP is used to return the length of the specified string. Syntax strlen ( string $string ) Parameter string(required)- This parameter represents the string which is measured for length. Return This function returns the length of the string on success or returns 0 if the specified...

1 minute read.

PHP array_product() Function

PHP array_product() Function The array_product() function in PHP calculates the product of values in an array. Syntax array_product ( array $array ) Parameter array(required)- This parameter represents the input array. Return This function returns the product of the array elements as an integer or float....

1 minute read.

PHP is_long() Function

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

1 minute 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 strrrev() Function

PHP strrrev () Function The strrev() function in PHP reverses a string. Syntax strrev ( string $string ) Parameter string(required)- This parameter represents the input string to be reversed. Return This function returns the reversed string. Example 1 Output Initial string: Hello...

1 minute read.

PHP array_fill() function

PHP array_fill() function The array_fill() function in PHP fills an user-defined array with num entries of the value that of specified values parameter, where the keys start at the start_index parameter. Syntax array_fill ( int $start_index , int $num , mixed $value ) Parameter start_index(required)- This parameter represents the first index of the...

1 minute read.

PHP settype() Function

PHP settype() Function The settype() function in PHP is used to set the type of variable var to type. Syntax settype ( mixed &$var , string $type ) Parameter var(required)- This parameter represents the variable being converted. type(required)- This parameter represents the type of variable that is needed.  The possible values of type are...

1 minute read.

PHP reset() Function

PHP reset() Function The reset() function in PHP is used to set the internal pointer of an array to its first element and returns the value of the first array element. Syntax reset ( array &$array )  Parameter array(required)- This parameter represents...

1 minute read.

PHP Indexed Array

In PHP, arrays are linear data structures that allow to store multiple elements of the same type under a single variable which helps in saving the time and effort of...

3 minutes read.

PHP $ and $$ Variables

PHP $var variable is a normal variable that stores any value. PHP $$var variable is a reference variable that stores the value of$var inside it. Let us take an example. Example 1 <?php $x =...

1 minute read.

PHP is_int() Function

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

1 minute read.

PHP Comment

PHP commentis a line of code that is not executed. It is often used by developer to understand the code. PHP support single and multiple comments. In other language like C,...

1 minute read.

PHP ucwords() Function

PHP ucwords() Function The ucwords() function in PHP converts the first character of each word in the specified string to uppercase. It is a binary-safe function. The related function are as follows: ucfirst()lcfirst()strtoupper()strtolower() Syntax ucwords ( string $str [, string $delimiters ] ) Parameter str(required)- This parameter represents...

1 minute read.