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.

For example:

include("user_auth.php");

 

require("scripts/db_connect.php");
click below button to copy the code. php tutorial - team

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

 php-include

Learn php - php tutorial - php-include - php examples - php programs

This is how the include statement of PHP is used:

include("define_variables.php");
click below button to copy the code. php tutorial - team

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-include-inner

Learn php - php tutorial - php-include-inner - php examples - php programs

The following PHP code is used to include inner directory:

include("scripts/define_variables.php");
click below button to copy the code. php tutorial - team

After including the file, the included file variables are used just like in above example:

echo "The value of variable x =". $x;

echo "<br>The value of variable y =". $y;
click below button to copy the code. php tutorial - team

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:

 php-include-calling-inner

Learn php - php tutorial - php-include-calling-inner - php examples - php programs

The include statement in above example used as this:

include("../define_variables.php");
click below button to copy the code. php tutorial - team

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:

 php-require

Learn php - php tutorial - php-require - php examples - php programs

The require statement is used as follows:

require("define_variables.php");
click below button to copy the code. php tutorial - team

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):

 php-include-warning

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:

The value of variable x =

The value of variable y =
click below button to copy the code. php tutorial - team

Now see what happens in case of require statement:

 php-require-fatal-error

Learn php - php tutorial - php-require-fatal-error - php examples - php programs

The following line generated a fatal error:

require("define_variables12.php");
click below button to copy the code. php tutorial - team

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.


Related Searches to PHP include VS require