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.
- Function include ()
- 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:
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):
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):
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
You should read it
- File & I / O in PHP
- Header File in C
- PHP functions
- Save time with these text formatting functions in Microsoft Excel
- FUNCTION (Function) in SQL Server
- Function tmpfile () in C
- How to use the IFS function in Excel 2016
- How to use functions in Less CSS
- The rewind () function in C
- Function in C / C ++
- Function open () in Python
- Function tmpnam () in C
Maybe you are interested
The whole scene of meteorites hitting the Earth during the past 33 years, why can't we feel it? The most beautiful meteor shower of the year and amazing astronomical phenomena in August Concerned about security issues, SpaceX banned employees from using Zoom How to Install PHPMailer 8 ways to win people's hearts Scientists successfully cultivate the perfect human vascular system in the laboratory