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 array_intersect_key() function

The array_intersect_key () function in PHP is used to compute the intersection of arrays using keys for comparison. It returns an array containing all the entries from array1 which have keys that are present in...

1 minute read.

PHP key_exists() Function

PHP key_exists() Function The key_exists() function in PHP checks if the specified key or index exists in the array or not. Syntax key_exists ( mixed $key , array $array )  Parameter key(required)- This parameter represents the value to check. array(required)- This parameter signifies an input array with...

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

PHP print() Function The print() function in PHP is used to output one or more string. Syntax print ( string $arg ) Parameter arg(required)- This parameter represents the input data. It can take one or more arguments. Return This function returns an integer value 1. Example...

1 minute read.

PHP array_column() Function

PHP array_column() Function The array_column() function in PHP is used to return the values from a single column specified in the input array. Syntax array_column (array $input, mixed $column_key [, mixed $index_key]) Parameter input(required)- This parameter represents a multi-dimensional array or an array of...

1 minute read.

PHP is_object() Function

The is_ object() function in PHP finds whether the specified variable is an object or not. It returns a Boolean value TRUE if the specified var is an object else it returns FALSE. Syntax is_object(mixed$var) var(required)- This parameter represents the...

1 minute read.

PHP strspn() Function

PHP strspn() Function The strspn() function of PHP finds the length of the initial segment of string that contains only characters from the mask parameter. Syntax strspn(string$subject,string$mask[,int$start[,int$length]] ) Parameter subject(required)- This parameter represents the input string. mask(required)- This parameter specifies the characters to find. start (optional)- It signifies the position in the string to start...

1 minute read.

PHP str_getcsv() Function

PHP str_getcsv() Function The str_getcsv() function in PHP parses a CSV string into an array and returns an array containing the fields read. Syntax str_getcsv ( string $input [, string $delimiter [, string $enclosure [, string$escape ]]] ) Parameter input(required)- This parameter represents the string to parse. delimiter(optional)- This parameter signifies...

1 minute read.

PHP strchr() Function

PHP strchr() Function The strchr() function in PHP searches for the first occurrence of the specified string inside another string. It is a binary-safe case -sensitive function and is the alias of strstr() function. Syntax strstr ( string $haystack , mixed $needle [, bool $before_needle] ) Parameter haystack(required)-...

1 minute read.

PHP next() Function

PHP next() Function The next() function in PHP advances or moves the internal array pointer one place forward and returns the next array value. Syntax next ( array &$array ) Parameter array(required)- This parameter represents the input array. Return This parameter returns the next array value...

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.

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

PHP array_merge_recursive() Function The array_merge_recursive() function  in PHP merges the elements of one or arrays together and returns the resulting array. Syntax array_merge_recursive (array $array  , [ array $... ] ) Parameter array(required)- This parameter specifies the input array ….(optional)- It represents more arrays to...

1 minute read.

PHP Array Functions

There are various array functions in PHP. They are used to access and manipulate the elements ofan array. Following are functions of array. Array Function Description array() It is used to create an array. array_change_key_case() It is...

3 minutes read.

PHP Environment Setup

To install PHP, We have to installed AMP(Apache, MySQL and PHP) software. There are various AMP software available in the market. XAMPP (Cross-Platform, Apache, MySQL, PHP and Perl) for windows. ...

2 minutes read.

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...

3 minutes read.

PHP trim() Function

PHP trim() Function The trim() Function in PHP strips the whitespace or other characters from the beginning and the end of the given string. The trim() function strips the following characters: " " - an ordinary space."\t"- a tab."\n"- a new...

1 minute read.

PHP stripslashes() Function

PHP stripslashes() Function The stripslashes() Function in PHP removes backslashes or un-quotes a quoted string. Syntax stripslashes ( string $str ) Parameter str(required)- This parameter signifies the input string. Return This function returns a string with backslashes stripped off and the double backslashes (\\) are made...

1 minute read.

PHP strcasecmp() Function

PHP strcasecmp() Function The strcasecmp() function in PHP compares two strings. It is a binary-safe case-insensitive string function. Syntax strcasecmp ( string $str1 , string $str2 ) Parameter str1(required)- This parameter represents the first string. str2(required)- This parameter represents the second string. Return This function returns a value less than( <...

1 minute read.

PHP uasort() Function

PHP uasort() Function The uasort() function in PHP sorts an array with a user-defined comparison function and maintains the index association. Syntax uasort ( array &$array , callable $value_compare_func ) Parameter array(required)- This parameter represents the input array. value_compare_func(required)- This parameter represents a string that describe a...

1 minute read.