php tutorial - PHP include VS require - php programming - learn php - php code - php script
The purpose of include and require statements in PHP
The include and require PHP statements are used to include the specified files in the calling file/program.
- The code of the specified file will be copied to the calling program before execution by using the include or require statements.
- This feature is particularly useful if you have to use the same code of PHP, HTML etc. in multiple pages. For example, you have to establish the 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 DB connection in it. Juts 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 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.
In case of include statement:
If the specified file is not found, the program will generate a warning message. In that case, the execution of rest of the program will continue and users will see only a warning message (if settings are made in the configuration file). This may be useful in case the included file does not impact the other program sections.
In case of require statement:
If a given file is not found after using the PHP require statement, the program will generate a fatal error (E_COMPILE_ERROR). 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 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 the PHP include statement
In this example, two PHP files are created. The first file, define_variables.php contains two variables. Where the other file calls this file and displays the variable values by using the echo statement. Both files are placed in the same directory.
Learn php - php tutorial - php-include - php examples - php programs
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.
Learn php - php tutorial - php-include-inner - php examples - php programs
The following PHP code is used to include inner directory:
After including the file, the included file variables are used just like in above example:
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:
Learn php - php tutorial - php-include-calling-inner - php examples - php programs
The include statement in above example used as this:
To go one step back use “../” before the file name.
Using require statement to include files example
In all above example, simply replace the word include by require to execute the require statement of PHP.
Using the same PHP file as in above example, this is how the require statement will work:
Learn php - php tutorial - php-require - php examples - php programs
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 would not halt. In this example, I mistyped the above file name and see the output (first in case of include statement):
Learn php - php tutorial - php-include-warning - php examples - php programs
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:
Now see what happens in case of require statement:
Learn php - php tutorial - php-require-fatal-error - php examples - php programs
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.