PHP Tutorial
PHP allows us to create various elements and functions, which are used several times in many pages.
File inclusion that helps to include files in various programs and saves the effort of writing code multiple times.
PHP allows you to include file so that a page content can be reused many times.
It is very helpful to include files when you want to apply the same HTML or PHP code to multiple pages of a website with the include or require statement.
This function is used to copy all the contents of a file called within the function.
This happens before the server executes the code.
include 'filename ';
Or
include ('filename');
Let's see a simple PHP include example.
<li>MySQL</li>
<li>HTML</li>
<li>CSS</li>
<li>Java Script</li>
<?php
echo "Programming List";
include("list.html");
?>
It will produce the following result −
Programming List
PHP require is similar to include, which is also used to include files.
The only difference is that it stops the execution of script if the file is not found whereas include doesn't.
require 'filename';
Or
require ('filename');
Let's see a simple PHP include example.
<li>MySQL</li>
<li>HTML</li>
<li>CSS</li>
<li>Java Script</li>
<?php
echo "Programming List";
require("list.html");
?>
It will produce the following result −
Programming List
Both include and require are same. But if the 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.