Hi guys, in this article we are going to understand how to include another file in PHP. First of all, we need to understand the concept of Including a file; you can insert the content of one PHP file into another PHP file before
the server executes it, with the include() or require() function. The whole process is done under XAMPP server.
The Two functions are identical
- include() generates a warning, but the
script will continue execution.
- require() generates a fatal error, and the
script will stop.
include() function in PHP
The functionality of the include function is like it involves to take all the
content in a specified file and includes it in the current file. This function
is used to generates a warning, but the script will continue execution.
Let us understand the concept with the some example to clarify the function,
consider that you have a standard header file, called "function.php". To
include the header file in a page, use the include() function.
<html>
<body
bgcolor="pink">
<h3>WORLD
CRICKET BIGGEST EXCITEMENT<h3><hr
/>
<?php
function
writeName()
{
echo
"India vs
Australia";
}
echo
"Upcoming
Tour : ";
writeName();
?>
</body>
</html>
Saved it as function.php
Now the script given
below is the implementation part of the include() function
<html>
<body>
<?php
include("function.php");
?>
// This is include function that is using the function.php
<h1>Welcome
to Sports page!</h1>
<p>Former
Australian wicketkeeper-batsman Adam Gilchrist expects the forthcoming
India-Australia Test series to be an exciting affair. However, he refuses to
predict the outcome of the much-awaited series, starting with the Boxing Day
Test at the Melbourne Cricket Ground.</p>
<p>Former
Pakistan skipper Wasim Akram is baffled by the Indian selection committee's
decision to opt for Abhimanyu Mithun over an in-form Irfan Pathan for the
much-awaited upcoming tour of Australia.</p>
</body>
</html>
Output of the above php script
Open XAMPP server them run the services Apache & MySQL. Type
http://localhost/yourfoldername/include.php on your browser.
require() function in PHP
The functionality of require
function is identical to include(), except that it handles errors
differently. This function is used to generates a fatal error, and the script
will stop.
Error handing by require() & include()
Firstly, we will understand that how will the include() tackle with errors,
if we run the php script on the browser it will display output with warnings. Let us see that whole concept with example.
<html>
<body>
<?php
include("wrongfile.php");
?>
// This is include
function that show output with warnings.
<h1>Welcome
to Sports page!</h1>
<p>Former
Australian wicketkeeper-batsman Adam Gilchrist expects the forthcoming
India-Australia Test series to be an exciting affair. However, he refuses to
predict the outcome of the much-awaited series, starting with the Boxing Day
Test at the Melbourne Cricket Ground.</p>
<p>Former
Pakistan skipper Wasim Akram is baffled by the Indian selection committee's
decision to opt for Abhimanyu Mithun over an in-form Irfan Pathan for the
much-awaited upcoming tour of Australia.</p>
</body>
</html>
saved it by include.php
Output
Open XAMPP server the run the services Apache & MySQL. Type
http://localhost/yourfoldername/include.php on your browser.
Secondly, same concept with require(), if we run the above script with require()
we will find some difference from above output, difference is that
if we run the include.php script on the browser it
will only display warning & fatal errors, not the output.
<html>
<body>
<?php
require("wrongfile.php");
?>
// This is require
function that show warning & fatal error only not output.
<h1>Welcome
to Sports page!</h1>
<p>Former
Australian wicketkeeper-batsman Adam Gilchrist expects the forthcoming
India-Australia Test series to be an exciting affair. However, he refuses to
predict the outcome of the much-awaited series, starting with the Boxing Day
Test at the Melbourne Cricket Ground.</p>
<p>Former
Pakistan skipper Wasim Akram is baffled by the Indian selection committee's
decision to opt for Abhimanyu Mithun over an in-form Irfan Pathan for the
much-awaited upcoming tour of Australia.</p>
</body>
</html>
Output
Open XAMPP server the run the services Apache & MySQL. Type
http://localhost/yourfoldername/include.php on your browser.
The given statement is not executed, because
the script execution stopped after the fatal error.