Insert file in PHP

You can insert the content of a PHP file into another PHP file before the Server executes it. There are two functions in PHP that can be used to do this.

You can insert the content of a PHP file into another PHP file before the Server executes it. There are two functions in PHP that can be used to do this.

  1. Function include ()
  2. Require function ()

This is a strong point of PHP that helps in creating functions, headers, footers or elements that can be reused in multiple pages. This will help developers easily change the layout of the web. If any changes are required, instead of having to change thousands of files, just change the included file.

The include () function in PHP

The include () function takes all the text in the specified file and copies them into the file that uses the include function. If there are any problems during file loading, the include () function generates an alert but the script continues executing.

Suppose you want to create a general menu for the Website. Then create a file as menu.php in htdocs with the following content:

 Home - 
PHP -
JAVA -
HTML

Now create as many pages as you want and insert this file to create the header. For example, test.php may have the following content.




The example illustrates how to include files in PHP!



Save the above program in a file named test.php in htdocs , then open the browser and type the address http:/// localhost: 8080 / test.php will result:

Insert file in PHP Picture 1Insert file in PHP Picture 1

The require () function in PHP

The function require () takes all the text in the specified file and copies them into the file using the require function. If there is any problem during file loading, the require () function generates a fatal error (Fatal Error) and prevents the execution of the script.

So there is no difference between require () and include () besides they handle error conditions. We encourage you to use require () function instead of include (), because the script should not continue executing if the file is missing or the name is wrong.

You can use the above example with the require () function and it will generate the same result. But if you try to follow the following two examples, and if with a file does not exist, you will get different results.




The example illustrates how to include files in PHP!



Save the program in a file named test.php in htdocs , then open the browser and type in the address http:/// localhost: 8080 / test.php will result (done on Google Chrome):

Insert file in PHP Picture 2Insert file in PHP Picture 2

Now try the above example with the require () function in PHP.




The example illustrates how to include files in PHP!



The executable file this time pauses and does not display anything.

Save the program in a file named test.php in htdocs , then open the browser and type in the address http:/// localhost: 8080 / test.php will give the result (I do it on Google Chrome):

Insert file in PHP Picture 3Insert file in PHP Picture 3

Note - You may receive a warning message or error message or nothing. This depends on your PHP Server configuration.

Follow tutorialspoint

Previous lesson: String (String) in PHP

Next lesson: File & I / O in PHP

5 ★ | 1 Vote