PHP Include and Require Statements

PHP include statement is used to fetch the code contained in a file and display or include it in the second file. In short, you can put some code in one file and can display it in many files.

Lesson Goals

  • Write reusable files and include them in multiple pages.
  • Write user-defined functions.
  • Create a library of user-defined form-validation and form-entry functions.

Including files is very helpful when you need to include the identical PHP, HTML, or text on multiple pages of a web site.

Keeping up or redesigning an impressive site is not straightforward in the event that you need to make updates to the header, navigation or footer sections.

You will edit every page in parts. This will take quite a while particularly depending on if you have number of pages to update.

PHP include() function permit you to import HTML/PHP code from external files to your present PHP script.

PHP has four commands that might be include code from an external file:

    • PHP include() function – creates a warning if the external file is absent, however the script will proceed execution.
    • PHP include_once() function – anticipate the same file from being included more than once in a page. In the event that the external file is lost, creates a warning and attempts to keep handling the script.
    • PHP require() function – If the external file is absent, produces a fatal error, and the script will stop.
    • PHP require_once() function -counteract the same file from being embedded more than once in a page. Provided that the external file is lost, quits preparing the script and tosses a fatal error.

PHP include() Function

PHP include() Function assists you in effortlessly redesigning all your pages from one file, which safeguards time and work.

Syntax for PHP include() Function:

 include(file_path);

PHP include Examples:

Example 1

external_file.php 
<?php 
  print "This is the second content"; 
?>
 index.php 
 <?php 
  print "This is the first content <br />"; 
  include ("external_file.php"); 
?>

Output will be:

 This is the first content 
 This is the second content

PHP include_once() Function

PHP include_once() Function is comparative to the PHP include() Function, with the main distinction being that if the code from a file has as of recently been included, it won’t be included again.

PHP include_once() Function will prevent problems with function redefinitions, variable value reassignments, etc.

Example on include_once() in PHP:

 data.php 
<?php
echo "Hello <br />";   // this will print Hello with one line break
?>
 inc.php
<?php
include "data.php";        // this will display Hello once
include "data.php";        // this will display Hello once
include "data.php";        // this will display Hello once
 
include_once "data.php"; // this will not display as the file is already included. 
include_once "data.php";  // this will also not display as the file is already included.
?>

The output will be:

 Hello
 Hello
 Hello

So here in the above code the echo command displaying Hello will be displayed three times and not five times. PHP include_once() Function will not include the data.php file again.

PHP require()

PHP require() Function is comparative to the PHP include() Function, the main contrast between the PHP include() Function and PHP require() Function is that the require() Function triggers a fatal error when the file included can’t be discovered, while the PHP include() Function just issues a cautioning.

Syntax for PHP require() Function:

require(file_path);

Example on PHP require() Function:

 main.php
<?php require("header.php"); echo "Just some random text!"; ?>

Now the header.php file doesn’t exist, so you get the following error:

Warning: require(header.php) [function.require]: failed to open stream: No such file or directory in C:\mywebsite\main.php 
on line 2 Fatal error: require() [function.require]: Failed opening required 'header.php' (include_path='.;C:\php5\pear') in 
C:\mywebsite\main.php on line 2

PHP require_once() Function

PHP require_once() Function is identical to PHP require() function except that PHP will check if the file has already been included, and if so, not include (require) it again.

If PHP require_once() function fails it will generate a fatal error and the execution will stop.

Example on PHP require_once() Function:

data.php
<?php
echo "Hello <br />";   // this will print Hello with one line break
?>
inc.php
<?php
require "data.php";        // this will display Hello once
require "data.php";        // this will display Hello once
require "data.php";        // this will display Hello once
 
require_once "data.php"; // this will not display as the file is already included. 
require_once "data.php";  // this will also not display as the file is already included.
?>

The output will be:

 Hello
 Hello
 Hello

And if file doesn’t exist output will be:

 Warning: require_once(data.php) [function.require-once]:
failed to open stream:
No such file or directory in C:\mywebsite\inc.php on line 6

Fatal error: require_once() [function.require]:
Failed opening required data.php'
(include_path='.;C:\php5\pear')
in C:\mywebsite\inc.php on line 6

auto_prepend_file and auto_append_file

The configuration file, php.ini, contains settings for automatic prepending and appending files to every PHP script. These settings are auto_prepend_file and auto_append_file. By default, they contain no values; however, they can be set to point to a file using absolute paths as follows:

; Automatically add files before or after any PHP document.
auto_prepend_file = “c:/files/include/runbefore.inc”
auto_append_file = “c:/files/include/runafter.inc”

The auto_prepend_file directive can be used to include application-wide variables such as database connection strings or common file paths. The auto_append_file directive can be used for cleaning up code or for outputting debugging information to the browser.

Note that it is not possible to set different auto-prepend and auto-append files for different directories or different scripts.

A Note on Security

If included files are under the web root, they can be accessed just as any other file can. If they have an extension such as ink, then the browser may display them as plain text. With other extensions, the browser may attempt to download the file. If the included file is a PHP file and a user navigates to it, the server will try to process the file and may return errors. As a precaution, you may want to place your included files in a directory above or outside of the web root. This will prevent users from accessing the files directly.

Add a Comment

Your email address will not be published. Required fields are marked *

ABOUT CODINGACE

My name is Nohman Habib and I am a web developer with over 10 years of experience, programming in Joomla, Wordpress, WHMCS, vTiger and Hybrid Apps. My plan to start codingace.com is to share my experience and expertise with others. Here my basic area of focus is to post tutorials primarily on Joomla development, HTML5, CSS3 and PHP.

Nohman Habib

CEO: codingace.com

Request a Quote









PHP Code Snippets Powered By : XYZScripts.com