WEB DEVELOPER SITE
HTMLCSSJAVASCRIPTSQLPHPBOOTSTRAPJQUERYANGULARXML
 

PHP 5 Include Files


The incorporate (or require) proclamation takes all the content/code/markup that exists in the predetermined record and duplicates it into the record that utilizes the incorporate statement.

Including records is extremely valuable when you need to incorporate the equivalent PHP, HTML, or content on numerous pages of a website.


PHP incorporate and require Statements

It is conceivable to embed the substance of one PHP record into another PHP document (before the server executes it), with the incorporate or require statement.

The incorporate and require explanations are indistinguishable, aside from upon failure:

  • require will deliver a lethal blunder (E_COMPILE_ERROR) and stop the script
  • include will just deliver a notice (E_WARNING) and the content will continue

So, in the event that you need the execution to go on and show clients the yield, regardless of whether the incorporate record is missing, utilize the incorporate explanation. Something else, if there should be an occurrence of FrameWork, CMS, or a complex PHP application coding, dependably utilize the expect articulation to incorporate a key document to the stream of execution. This will help abstain from trading off your application's security furthermore, honesty, just in the event that one key record is unintentionally missing.

Including records spares a ton of work. This implies you can make a standard header, footer, or menu record for all your site pages. At that point, when the header should be refreshed, you can as it were refresh the header incorporate file.

Syntax

incorporate 'filename';

or

require 'filename';

PHP incorporate Examples

Example

Assume we have a standard footer document called "footer.php", that resembles this:

<?php
echo "<p>Copyright &copy; 2018-" . date("Y") . " welookups.com</p>";
?>

To incorporate the footer record in a page, utilize the incorporate statement:

Example

<html>
<body>

<h1>Welcome to my home page!</h1>
<p>Some text.</p>
<p>Some more text.</p>
<?php incorporate 'footer.php';?>

</body>
</html>

Example 2

Assume we have a standard menu record called "menu.php":

<?php
echo '<a href="/default.asp">Home</a> -
<a href="/html/default.asp">HTML Tutorial</a> -
<a href="/css/default.asp">CSS Tutorial</a> -
<a href="/js/default.asp">JavaScript Tutorial</a> -
<a href="default.asp">PHP 7 Tutorial</a>';
?>

All pages in the Web website should utilize this menu document. you can utilize css for style

Example 3

<html>
<body>

<div class="menu">
<?php incorporate 'menu.php';?>
</div>

<h1>Welcome to my first website!</h1>
<p>Some text.</p>
<p>Some more text.</p>

</body>
</html>

Example 3

Assume we have a record called "vars.php", with a few factors defined:

<?php
$color='blue';
$house='house';
?>

Then, in the event that we incorporate the "vars.php" record, the factors can be utilized in the calling file:

Example

<html>
<body>

<h1>Welcome to my home page!</h1>
<?php incorporate 'vars.php';
reverberation "I have a $color $house.";
?>

</body>
</html>

PHP incorporate versus require

The require proclamation is likewise used to incorporate a document into the PHP code.

However, there is one major contrast among incorporate and require; when a document is incorporated with the include explanation and PHP can't discover it, the content will keep on execute:

Example

<html>
<body>

<h1>Welcome to my first web site!</h1>
<?php incorporate 'noFileExists.php';
reverberation "I have a $color $house.";
?>

</body>
</html>

you can utilize another examplerequire proclamation, the reverberation proclamation won't be executed in light of the fact that the content execution kicks the bucket after the require articulation restored a deadly error:

Example

<html>
<body>

<h1>Welcome to my website!</h1>
<?php require 'noFileExists.php';
reverberation "I have a $color $house.";
?>

</body>
</html>