Difference between Include and Require in PHP
To include other files containing PHP code in the same PHP file, the statements include and require can be used.
Example :
// to include a PHP file: include 'myfile.php'; // to require a PHP file: require 'myfile.php';
Both absolute and relative paths can be specified. If no path is specified, the file is first searched for in the path under include_path, then in the directory of the file that contains the include/require.
Unlike other languages, include and require do not necessarily have to be at the beginning of the file. Placement in the middle of the code is also allowed. Similarly, the included files do not necessarily have to contain classes, but can also contain procedural code that is executed immediately.
To prevent files from being included multiple times, you can use include_once and require_once, which only load a file once, no matter how often they are called.
[st_adsense]
Difference between include and require
The difference between include and require is only in their behavior if the specified file cannot be found. If this happens, require immediately terminates the script execution, while include only generates a warning and executes the rest of the code. In general, include is used more often, but depending on the application, require may also be useful.
- include: Warning if the file is not found
- require: Fatal Error (exit the script) if the file is not found
Example 1:
An example file in which some code has been replaced:
PHP: base.php
<?php // base.php echo("This message comes from page base.php \n"); ?>
PHP: test.php : Use of the code:
<?php // test.php echo("This message is from test.php \n"); include 'base.php'; require 'base.php'; ?>
Output:
This message is from test.php This message comes from page base.php This message comes from page base.php
As you can see, includes/requires do not necessarily have to be at the beginning of the file.
[st_adsense]
Example 2: Include function from another file
An example file with a function that outputs the numbers from 1 to 5:
PHP: myfunction.php
<?php // myfunction.php function show1to5() { for ($i=0; $i<=5; $i++) { echo($i . "\n"); } } ?>
This file (test.php) integrates the previous function via include:
PHP: test.php
<?php // test.php include 'myfunction.php'; echo("All numbers from 0 to 5: \n"); show1to5(); ?>
Output of test.php:
All numbers from 0 to 5: 0 1 2 3 4 5[st_adsense]
Example 3: Include a class from another file
An example file with a simple class:
PHP: Welcome.php
<?php // Welcome.php class Welcome { public function msg() { echo("Welcome to StackHowTo.com \n"); } } ?>
Now this class is used in a completely different file:
PHP: test.php
<?php // test.php include 'Welcome.php'; $w = new Welcome(); $w->msg(); ?>
Output of test.php:
Welcome to StackHowTo.com
Example 4: Difference between Include and Require
The difference between include and require is shown below. While include only generates a warning if the specified file is not found, require aborts the entire sequence of the script with a fatal error.
1- Behavior with “include”, if the file is not found:
PHP: test.php
<?php // test.php // show all possible errors error_reporting(E_ALL); // Include a nonexistent file include 'file_does_not_exist.php'; echo("END"); ?>
Output:
Warning: include(): Failed opening 'file_does_not_exist.php' for inclusion (include_path='.') in .test.php on line 8 END[st_adsense]
2- Behavior with “require”, if the file is not found:
PHP: test.php
<?php // test.php // show all possible errors error_reporting(E_ALL); // Require a nonexistent file require 'file_does_not_exist.php'; echo("END"); ?>
Output:
Fatal error: require(): Failed opening 'file_does_not_exist.php' for inclusion (include_path='.') in .test.php on line 8[st_adsense]