Home » Web development » PHP » include or require function in PHP with practical examples

include or require function in PHP with practical examples

Website design require header,footer,side bar,right bar, ad bar on multiple pages.It is not possible to change these on thousand of web pages whenever we want to change anything. Include function in php  solve this problem. We can  use include function in PHP and include the content of a PHP file into another PHP file before the server executes it.So If there is any change required then instead of changing thousand of files just change included file.
PHP has two functions which can be used to included one PHP file into another PHP file.
The include() Function
The require() Function

Both the function has different working .

The include (or require) statement takes all the text/code/markup that exists in the specified file and copies it into the file that uses the include statement

include function in PHP

The include() function takes all the text in a specified file and copies it into the file that uses the include function.

Syntax

include '<file name>'

Example

<?php
include '/home/test/public_html/coded/fbscript.php';
?>
  • If there is any problem in loading a file then the include() function generates a warning but the script will continue execution.
  • If in above example if the fbscripts.php does not exists,then still code will run  and it will just generate warning

Require function in php

The require() function takes all the text in a specified file and copies it into the file that uses the require function.

Syntax

require '<file name>'

Example

<?php
require '/home/test/public_html/coded/fbscript.php';
?>
  • If there is any problem in loading a file then the require() function generates a fatal error and halt the execution of the script.
  • If in above example if the fbscripts.php does not exists,then still code will halt
See also  How to reset MySQL root password on your Linux server

Useful examples for Include function in PHP

<!doctype html> 
<html class="no-js" lang="en">

 
<head>
<meta charset="utf-8" />
<title>Example Site</title>
<?php include '/home/app//public_html/comcode/stylesheets_depth.php'; ?>

<?php include '/home/app//public_html/comcode/javascript.php'; ?>
</head>
<body>

 
<?php include_once("/home/app//public_html/comcode/analytics.php") ?>
<?php include '/home/app//public_html/comcode/brow-update.php'; ?>
<?php include '/home/app//public_html/comcode/facebooscript.php'; ?>
<?php include '/home/app//public_html/comcode/header1.php'; ?>

< text here>
</body>
<?php include '/home/app//public_html/comcode/footer.php'; ?>

</html>

The above page will run successfully still even if the php script are not present as we have used include

Useful examples for Require function in PHP

<?php 
require_once "/home/app/public_html/header1.php";

?>
<!doctype html>
<html class="no-js" lang="en">
<head>

<TITLE> Example site </TITLE>
<META NAME="revisit-after" CONTENT="1">
<?php include '/home/app/public_html/comcode/stylesheets.php'; ?>
<?php include '/home/app/public_html/comcode/javascript.php'; ?>

</head>

<body>
<?php include_once("/home/app/public_html/comcode/analytics.php") ?>
<?php include '/home/app/public_html/comcode/brow-update.php'; ?>
<?php include '/home/app/public_html/comcode/facebookscript.php'; ?>
<?php include '/home/app/public_html/comcode/header2.php'; ?>

 
<text here>

</body>
<?php include '/home/app/public_html/comcode/footer.php'; ?>

</html>

The above will fail if the first header file is not available as it uses the require function

Hope you like post on include function in PHP and require function in PHP

Also Reads
How to run php file in windows : This post talks about how we can php file on the the window and do the testing before moving to the web server
uses of PHP : This has detailed description why PHP is so much used through out the world.
first php program : This is good starting point for anybody starting with PHP and talk in good detail about how to write your PHP program and execute it
Define constant in PHP
https://en.wikipedia.org/wiki/PHP

Leave a Comment

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

Scroll to Top