- PHP Basics
- PHP Home
- PHP Environment Setup
- PHP Getting Started
- PHP Basic Syntax
- PHP echo
- PHP print
- PHP echo Vs print
- PHP Comments
- PHP Data Types
- PHP Variables
- PHP Variable Scope
- PHP gettype()
- PHP Constants
- PHP Operators
- PHP Program Control
- PHP Decision Making
- PHP if-elseif-else
- PHP switch
- PHP Loops
- PHP for Loop
- PHP while Loop
- PHP do-while Loop
- PHP foreach Loop
- PHP break & continue
- PHP Popular Topics
- PHP Arrays
- PHP print_r()
- PHP Strings
- PHP Functions
- PHP References
- PHP Object Oriented
- PHP Object Oriented
- PHP Classes & Objects
- PHP Member Variable
- PHP Member Function
- PHP Encapsulation
- PHP Data Abstraction
- PHP Inheritance
- PHP Constructor Destructor
- PHP Polymorphism
- PHP Web Developments
- PHP Web Developments
- PHP GET & POST
- PHP Read Requested Data
- PHP File Handling (I/O)
- PHP File Handling (I/O)
- PHP fopen() | Open File
- PHP Create a File
- PHP fwrite() | Write to File
- PHP fread() | Read File
- PHP feof()
- PHP fgetc()
- PHP fgets()
- PHP fclose() | Close File
- PHP unlink() | Delete File
- PHP Append to File
- PHP copy() | Copy File
- PHP file_get_contents()
- PHP file_put_contents()
- PHP file_exists()
- PHP filesize()
- PHP rename() | Rename File
- PHP fseek()
- PHP ftell()
- PHP rewind()
- PHP disk_free_space()
- PHP disk_total_space()
- PHP mkdir() | Create Directory
- PHP rmdir() | Remove Directory
- PHP glob() | Get Files/Directories
- PHP basename() | Get filename
- PHP dirname() | Get Path
- PHP filemtime()
- PHP file()
- PHP Advanced
- PHP Cookies
- PHP Sessions
- PHP Send Emails
- PHP Serialization
- PHP Namespaces
- PHP File Upload
- PHP Date and Time
- PHP Image Processing
- PHP Regular Expression
- PHP Predefined Variables
- PHP Error Handling
- PHP Debugging
- PHP and MySQLi Tutorial
- PHP and MySQLi Home
- PHP MySQLi Setup
- PHP MySQLi Create DB
- PHP MySQLi Create Table
- PHP MySQLi Connect to DB
- PHP MySQLi Insert Record
- PHP MySQLi Fetch Record
- PHP MySQLi Update Record
- PHP MySQLi Delete Record
- PHP MySQLi SignUp Page
- PHP MySQLi LogIn Page
- PHP MySQLi Store User Data
- PHP MySQLi Close Connection
- PHP connect_errno
- PHP connect_error
- PHP query()
- PHP fetch_row()
- PHP fetch_assoc()
- PHP fetch_array()
- PHP free_result()
- PHP error
- PHP prepare()
- PHP bind_param()
- PHP execute()
- PHP fetch()
- PHP store_result()
- PHP num_rows
- PHP bind_result()
- PHP get_result()
- PHP mysqli_result Class
- PHP Error Constants
- PHP mysqli_driver()
- PHP Misc
- PHP error_reporting()
- PHP Escape Special Characters
- PHP htmlspecialchars()
- PHP new
- PHP header()
- PHP getallheaders()
- PHP empty()
- PHP isset()
- PHP unset()
- PHP exit()
- PHP exit Vs break
- PHP include()
- PHP require()
- PHP include() Vs require()
- PHP AJAX & XML
- PHP AJAX
- PHP XML
- PHP File Handling Functions
- PHP abs()
- PHP Test
- PHP Online Test
- Give Online Test
- All Test List
PHP include() | Call Another PHP File to Execute
The PHP include() function is used when we need to include an external PHP file in a PHP script, to execute the script available in that external file, in the current PHP file. For example:
<?php include("header.php"); echo "Hey, PHP is Fun!<BR>"; echo "Is not it?"; ?>
The file header.php available in the current directory, and contains the following script:
<?php echo "-------Welcome to Company.com---------<HR>"; ?>
Therefore the output of above example on include() function in PHP, is shown in the snapshot given below:
Now the question is, what if the included file does not exists?
Let us find out the answer, using the example given below:
<?php include("unknown.php"); echo "Hey, PHP is Fun!<BR>"; echo "Is not it?"; ?>
Now the output is:
That is, since the file unknown.php is does not exists, therefore you are seeing the error. But the current script after the include() function still has executed. Use error_reporting(0); to turn off error reporting.
PHP include() Syntax
The syntax of include() function in PHP, is:
include "file";
Or
include 'file';
Or
include("file");
Or
include('file');
Note - The include() function produce warning (E_WARNING) message, in case if the specified file does not exists, and the script continues its execution.
Why to Use include() in PHP?
I do not know, what is your purpose of using the function include(). But, with the use of include() function, the same script need not to be written for every file. That is, if some PHP script like header and footer content are available in multiple files of your web application. Then better to go with include() function to write the header and footer content at once, in two PHP file. And include these two files, in each, to avoid writing multiple times.
Another benefit is, if you want to change the content of footer and/or header, then just change the content of that two files, it reflects the change in whole application or all files, where these files are included. For example:
<?php error_reporting(0); ?> <!DOCTYPE html> <html> <head> <style> * {box-sizing: border-box;} body {margin: 0;} .head {padding: 22px 0; font-size: 1.4em; text-align: center; background: maroon; color: white;} .navigation {display: flex; background-color: peru;} .navigation a {color: white; padding: 12px 16px; text-decoration: none; text-align: center;} .cont {display: flex; flex-wrap: wrap; min-height: 540px;} .menu {flex: 20%; background-color: #ccc; padding: 16px;} .content {flex: 60%; padding: 16px;} .right {flex: 20%; background-color: grey; color: white; padding: 16px;} .foot {padding: 22px 0; text-align: center; background: maroon; color: white;} @media screen and (max-width: 600px) {.cont, .navigation {flex-direction: column;}} </style> </head> <body> <?php include 'header.php'; ?> <div class="cont"> <div class="menu"> <p>----Some Menu----</p> </div> <div class="content"> <p>Content of the Page...</p> </div> <div class="right"> <p>----Right Side----</p> </div> </div> <?php include 'footer.php'; ?> </body> </html>
The content of the header.php file is:
<div class="head">CodesCracker</div> <div class="navigation"> <a href="#">MenuOne</a> <a href="#">MenuTwo</a> <a href="#">MenuThree</a> <a href="#">MenuFour</a> <a href="#">MenuFive</a> </div>
And the content of the footer.php file is:
<div class="foot"> <p>Some texts/links...</p> </div>
Now the output should be:
« Previous Tutorial Next Tutorial »
Like/Share Us on Facebook 😋