Monday, 20 July 2015

PHP File Inclusion

We can directly include the data of a PHP file into another php file inclusion before the server executes it. There are two PHP functions through which we can be used to include one PHP file into another PHP file.
  •     The include() Function
  •     The require() Function
Basically, both function are the same, both are used to insert useful codes written in other files, in the flow of execution.

 Include() Function:

In include command, include takes a file name and simply inserts that files contents into the script that issued the include command.
Include will only produce warning like (E_WARNING) and script will continue.
Now, suppose you want to create a common menu i.e. menu.php with the following content:

<html>
<body>
<a href=”http://www.fullyhelp.com/index.php”>Home</a> –
<a href=”http://www.fullyhelp.com/php”>PHP</a> –
<a href=”http://fullyhelp.com/c”>C</a> –
<a href=”http://www.fullyhelp.com/css”>CSS</a> <br />

Now create pages that you wanted and include this php file inclusion tutorial to create header. For example your test.php file can have following data.

<html>
<body>
<?php include(“menu.php”); ?>
<p>This is include PHP file!</p>
</body>
</html>

This will display the following output

Home-PHP-C-CSS 

Require() Function

In require function, require will produce a fatal error like(E_compile_ERROR) and it will halt the script. Now we take a above example using require function

<html>
<body>
<?php require(“xxmenu.php”); ?>
<p>This is include wrong PHP file!</p>
</body>
</html>

It will nothing display and stop the script.

No comments:

Post a Comment