PHP Include & Include_once
ADVERTISEMENTS
PHP Include Statement
- PHP Include statement is a file inclusion statement.
- Which used to add another text or code file into where you applied this include statement.
- Include statement is very useful that you have many pages contains the same code.
- So you can write a new file of this code and include every page to need this.
- Include statement includes file-based on the path of a file If file path did not give It checks in the calling script's own directory and the current working directory before failing.
Syntax of the php include the statement
<?php
include "file.php";
or
include("file.txt");
?>
Understand the include statement from this example:
1. A file named is 'home2.php'.
<?php
echo "Hello this is home 2 script";
echo "<br/>";
?>
2. The second file named is 'home.php'.
<?php
echo "Hello this is the home page script";
echo "<br/>";
include "home2.php"; // file 'home2.php' included on this line and file code is being executed in this script
?>
Output :
Hello this is the home page script
Hello this is home 2 script
PHP Include_once Statement
PHP Include_once statement checks that It is to be include script is already included or not the entire script.
Understand the include_once statement from this example
1. A file named is 'home2.php'.
<?php
echo "Hello this is home 2 script";
echo "<br/>";
?>
2. The second file named is 'home.php'.
<?php
include "home2.php"; //file included here
echo "Hello this is home page script";
echo "<br/><br/>";
include_once "home2.php"; // Checks 'home2.php' is already included, If included this inclusion will be skip.
?>