English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
In this tutorial, you will learn how to include and evaluate files in PHP.
The include() and require() statements can be used to include the code of one PHP file into another PHP file. Including a file produces the same result as copying the script from the specified file and pasting it at the location it is called.
You can save a lot of time and work by including files.-Just store the code block in a separate file and use include() and require() statements to include it at any location needed, without having to type the entire code block multiple times. A typical example is including header, footer, and menu files on all pages of the website.
The basic syntax of include() and require() statements can be given as follows:
include("path/to/filename"); or include "path/to/filename"; require("path/to/filename"); or require "path/to/filename";
Tip:Like print and echo statements, you can omit parentheses when using include and require statements, as mentioned above.
The following example will show you how to include the common header, footer, and menu code stored in separate "header.php", "footer.php", and "menu.php" files on all pages of the website. By using this technique, you can update all pages of the website immediately by changing just one file, thereby saving a lot of repetitive work.
<!DOCTYPE html> <html> <head> <title>Basic Tutorial</title> </head> <?php include "header.php"; ?> <?php include "menu.php"; ?> <h1>Welcome to our website(www.3codebox.com)!</h1> <p>Here, you will find a lot of useful information.</p> <?php include "footer.php"; ?> </html>
You may be wondering why we need require() if we can use the include() statement to include files. Typically, the require() statement behaves similarly to include().
The only difference is that the include() statement generates PHP warnings, but if the file to be included is not found, the script will continue to execute. However, the require() statement will generate a fatal error and stop the script execution.
<?php require "my_variables.php"; ?> <?php require "my_functions.php"; ?> <!DOCTYPE html> <html> <head> <title><?php displayTitle($home_page); ?></title> </head> <?php include "header.php"; ?> <?php include "menu.php"; ?> <h1>Welcome to our website(www.3codebox.com)!</h1> <p>Here, you will find a lot of useful information.</p> <?php include "footer.php"; ?> </html>
Note: If you include library files or include files that contain necessary functions and configuration variables for running the application (such asDatabase configuration file),it is recommended to use require() statements.
If you accidentally use include or require statements to include the same file multiple times (usuallyfunctionorclassFile) may cause conflicts. To prevent this, PHP provides include_once and require_once statements. These statements behave in the same way as include and require statements, with one exception:
Even if the file is included again, include_once and require_once statements will include the file only once, that is, if the specified file has been included in a previous statement, it will not be included again. To better understand how it works, let's look at an example. Suppose we have a 'my_functions.php' file with the following code:
<?php function multiplySelf($var){ $var *= $var; //Multiply the variable itself echo $var; } ?>
This is a PHP script that includes the 'my_functions.php' file.
<?php //Include file require "my_functions.php"; // Call the function multiplySelf(2); // Output: 4 echo "<br>"; //Include the file again require "my_functions.php"; // Call the function multiplySelf(5); // Doesn't execute ?>
When you run the above script, you will see an error message similar to the following:“Fatal error: Cannot redeclare multiplySelf()”The reason for this is that 'my_functions.php' is included twice, which means that the function multiplySelf() is defined twice, causing PHP to stop script execution and generate a fatal error. Now, rewrite the above example using require_once.
<?php // Including file require_once "my_functions.php"; // Call the function multiplySelf(2); // Output: 4 echo "<br>"; //Include the file again require_once "my_functions.php"; // Call the function multiplySelf(5); // Output: 25 ?>
As you can see, by using require_once instead of require, the script works as expected.