Published on Dec 19,2016
719 Views
Email Post

In PHP we can separate elements like functions, header, footers, left panel and right panel. These elements are stored as separate files and we can include these files in multiple pages. This is helpful when you have to include the same PHP script in multiple pages. If any changes are required then the developers can easily change the included files instead of changing thousands of files.

The two PHP functions available to include file are:

  • include() function.

  • require() function.

include() function:

We can include the content of one PHP file into another PHP file.

Example:

<a href=”http://www.mydomain.com/home.html”>Home</a>
<a href=”http://www.mydomain.com/services.html”>Services</a>
<a href=”http://www.mydomain.com/careers.html”>Careers</a>
<a href=”http://www.mydomain.com/contact.html”>Contact Us</a>

Save the above script as menu.php. Now, include the menu.php file in the other script using include() function.

Program:

<html>
<body>
<?php include(“menu.php);?>
<p> Other content of the file comes here </p>
</body>
</html>

Output:

Home – Services – Careers-Contact Us.

The  other content of the file comes here.

require() function:

The require() function takes all the text from a file and copies into the file which invoked the require() function. Both include() and require() function acts as same. The only difference is error handling. If there is any problem in the included file then the require() function generates a fatal error and halt the execution of the script.

Example:

<html>
<body>
<?php require(“menu.php);?>
<p> Other content of the file comes here </p>
</body>
</html>

Output:

Home – Services – Careers-Contact Us.

Other content of the file comes here.

Check out the related posts for more PHP tutorials.

Got a question for us? Mention them in the comments section and we will get back to you. 

Related Posts:

Get Started with PHP & MySQL

Differentiate PHP Code in HTML Script.

Functions in PHP

Share on

Browse Categories

Comments
0 Comments