PHP allows you to include files so that page content can be reused many times. There are two ways to include files in PHP.
Code Reusability: With the help of include and require construct, we can reuse HTML code or PHP script in many PHP scripts.
PHP include is used to include a file on the basis of a given path. You may use a relative or absolute path of the file. Let's see a simple PHP include an example.
File Name: first_menu.html
<a href="http://www.easytolearning.com">Home</a> |
<a href="http://www.easytolearning.com/php-tutorial">PHP</a> |
<a href="http://www.easytolearning.com/java-tutorial">Java</a> |
<a href="http://www.easytolearning.com/html-tutorials">HTML</a>
File: myFirst.php
<?php include("first_menu.html"); ?>
<h1>This is Main Page</h1>
Output:
Home | PHP | Java | HTML
This is Main Page
PHP require is similar to include. Let's see a simple PHP require example.
File Name: first_menu.html
<a href="http://www.easytolearning.com">Home</a> |
<a href="http://www.easytolearning.com/php-tutorial">PHP</a> |
<a href="http://www.easytolearning.com/java-tutorial">Java</a> |
<a href="http://www.easytolearning.com/html-tutorials">HTML</a>
File Name: first_require.php
<?php require("first_menu.html"); ?>
<h1>This is Main Page</h1>
Output:
Home | PHP | Java | HTML
This is Main Page
If file is missing or inclusion fails, include allows the script to continue but require halts the script producing a fatal E_COMPILE_ERROR level error.
© 2024 Easy To Learning. All Rights Reserved | Design by Easy To Learning