Including files in PHP
In PHP you can include one PHP file into another and can execute them on the server. For including files in PHP we can use following PHP functions:
- include()
- include_once()
- require()
- require_once()
Let’s see above function in details
To learn including files in PHP, let’s say there is a scenario where you want to create a menu bar in all of your web pages. In this case, you have to write menu bar code in all the web pages of your site repeatedly. You can overcome this repetition by using our file inclusion functions. You can write menu bar code in a separate file and then include that file in all the web pages where the menu bar is required.
1. include()
“include” function copies all the content of the file which we want to include into the desired file. We can use include function anywhere in our file. Wherever you use include function in the file the code of included file is get copied and displayed in a browser at that particular position.
If the file to be included is failed to load then a warning will be generated using include function. This will not cause the rest of script to stop and rest page will run. include function is widely used for including files in PHP.
Cookies in PHP are small text files which are saved by a browser in your computer. Cookies contain small pieces of data sent by the server, which can be accessed in any script.
Example:
Let’s take the same scenario of a menu bar inclusion in your web pages. Let’s create a file named header_menu.php and add below code in it and save.
<div> <span><a href="http://codingkala.com">Home</a></span> <span><a href="http://codingkala.com/php_tutorials">PHP Tutorials</a></span> <span><a href="http://codingkala.com/java_tutorials">Java Tutorials</a></span> </div>
Now add header_menu.php file in your web page using include function
<?php include('header_menu.php'); <div>File inclusion functionality in PHP</div> ?>
Note: Be sure to specify the file path in include function correctly as per the folder structure of your application.
2. include_once()
The include_once function allows us to include any other file only once in a current page. If the file is already been included in the page, then it will ignore the request to again include file in the page. If the file is failed to include then a warning will be generated.
<?php include_once('header_menu.php'); ?>
3. require()
For including files in PHP, “require()” function copies all the content of the file which we want to include, into the desired file. If there is an error including file then, in this case, a fatal error will get generated and rest of the scripts get terminated to execute.
We can use the same example of include function for the require function:
<div> <span><a href="http://codingkala.com">Home</a></span> <span><a href="http://codingkala.com/php_tutorials">PHP Tutorials</a></span> <span><a href="http://codingkala.com/java_tutorials">Java Tutorials</a></span> </div>
Now add header_menu.php file in your web page using the include function
Cookies in PHP are small text files which are saved by a browser in your computer. Cookies contain small pieces of data sent by the server, which can be accessed in any script.
<?php require('header_menu.php'); <div>File inclusion functionality in PHP</div> ?>
4. require_once()
“require_once” function allows us to include any other file only once in the current page. If the file is already been included in the page, then it will ignore the request to again include file in the page. If the file is failed to include then a fatal error will be generated and script terminates execution.
<?php require_once('header_menu.php'); ?>