What are include and require statements in PHP?
The “include” and “require” statements are used to include the specified files in the calling file/program.
For example:
include("user_auth.php"); require("scripts/db_connect.php");
- The code of the specified file is copied to the calling program before execution when using the include or require statements.
- This feature is particularly useful if you have to use the same code of PHP, HTML, etc. on multiple pages.
- For example, you have to establish a connection with MySQL or some other database from 100s of pages. You have to use the same piece of code to connect to a database server.
- Instead, you may create a file e.g. db_connection.php, and write the code of the DB connection in it.
- Just use the include or require statements in those 100s of pages and include that db_connection.php file.
- Not only it will simplify the code if in the future DB parameters are changed like user_id, password, etc. You have to change this only in the db_connection.php file.
- Similarly, you may use one file for the left, header, and footer bars/menus or widgets across the website by using include or require statements.
- The only difference between include and require statements is described below.
Difference between include and require statements
The include statement:
If the specified file is not found, the program will generate a warning message.
In that case, the execution of the rest of the program will continue and users will see only a warning message (if settings are made in the configuration file).
The require statement:
It will halt the script. So, the “require” statement may be used in scenarios where other sections of the program are dependent on the included file.
For example, it contains a DB script that connects to the database and fetches the rows from a MySQL table. This data is displayed in an HTML table.
The following section demonstrates the ways to include files by using PHP include and require statements.
An example of using PHP include statement
- In this example, two PHP files are created.
- The first file, define_variables.php contains two variables.
- The other file calls define_variables.php file and displays the variable values by using the echo statement.
- Both files are placed in the same directory.
PHP code and markup:
<!doctype html> <html> <head> <style> #timerdiv { background: green; height: auto; width:300px; border-radius: 15px; padding:20px; font-size:16px; color:#fff; } </style> </head> <body> <div id="timerdiv"><h3>The demo of PHP include</h3> < ?php include("define_variables.php"); echo "The value of variable x =". $x; echo "<br>The value of variable y =". $y; ?> </div> </body> </html>
Sample output:
This is how the include statement of PHP is used:
As included and calling files are placed in the same directory, you simply need to use the file name to include file.
An example of including an inner directory file
In this example, the same file containing the variables is stored in an inner directory, “scripts”. This is how the define_variables.php file can be included.
PHP and markup:
<!doctype html> <html> <head> <style> #timerdiv { background: #408080; height: auto; width:300px; border-radius: 15px; padding:20px; font-size:16px; color:#fff; } </style> </head> <body> <div id="timerdiv"><h3>PHP include with inner directory</h3> < ?php include("scripts/define_variables.php"); echo "The value of variable x =". $x; echo "<br>The value of variable y =". $y; ?> </div> </body> </html>
Sample output:
The following PHP code is used to include the inner directory:
After including the file, the included file variables are used just like in the above example:
echo "The value of variable x =". $x; echo "<br>The value of variable y =". $y;
An example to include a backward directory file
This time, the calling file exists in the inner directory. This is how you will include a file:
<!doctype html> <html> <head> <style> #timerdiv { background: #408080; height: auto; width:300px; border-radius: 15px; padding:20px; font-size:16px; color:#fff; } </style> </head> <body> <div id="timerdiv"><h3>PHP include with calling file in inner directory</h3> < ?php include("../define_variables.php"); echo "The value of variable x =". $x; echo "<br>The value of variable y =". $y; ?> </div> </body> </html>
Output:
The include statement in the above example is used as follows:
To go one step back use “../” before the file name.
Using require statement to include files example
In all the above examples, simply replace the word include by require to execute the require statement of PHP.
Using the same PHP file as in the above example, this is how the require statement will work:
<!doctype html> <html> <head> <style> #timerdiv { background: #800000; height: auto; width:300px; border-radius: 15px; padding:20px; font-size:16px; color:#fff; } </style> </head> <body> <div id="timerdiv"><h3>A demo of PHP require statement</h3> < ?php require("define_variables.php"); echo "The value of variable x =". $x; echo "<br>The value of variable y =". $y; ?> </div> </body> </html>
Output:
The require statement is used as follows:
What is the output if target file does not exist with include statement?
As mentioned earlier, if the specified file does not exist, the include statement will generate a warning message.
The program will keep on running or will not halt.
In this example, I mistyped the above file name and see the output (first in case of include statement):
<!doctype html> <html> <head> <style> #timerdiv { background: green; height: auto; width:300px; border-radius: 15px; padding:20px; font-size:16px; color:#fff; } </style> </head> <body> <div id="timerdiv"><h3>Output as File does not exists</h3> < ?php include("define_variables12.php"); echo "The value of variable x =". $x; echo "<br>The value of variable y =". $y; ?> </div> </body> </html>
Output:
You see, I called the define_variables12.php file that does not exist and it simply generated a warning message.
While the echo statement from the calling program still executed and displayed:
The value of variable x =
The value of variable y =
Now see what happens in the case of require statement:
<!doctype html> <html> <head> <style> #timerdiv { background: #FF0000; height: auto; width:300px; border-radius: 15px; padding:20px; font-size:16px; color:#fff; } </style> </head> <body> <div id="timerdiv"><h3>Output as File does not exist</h3> < ?php require("define_variables12.php"); echo "The value of variable x =". $x; echo "<br>The value of variable y =". $y; ?> </div> </body> </html>
Output:
The following line generated a fatal error:
Also, notice in the output, it did not display the echo statement messages as in the case of include statement. Instead, the program was halted.