PHP Tutorial
PHP Flow Control
PHP Functions
PHP String
PHP Array
PHP Date Time
PHP Object Oriented
Regular Expression
PHP Cookie & Session
PHP Error & Exception handling
MySQL in PHP
PHP File Directory
PHP Image Processing
The include
and require
statements are used to insert the contents of one PHP file into another PHP file before the server executes it. They are useful for reusing PHP code across multiple pages, which can be a significant time saver.
Here's a basic usage of each:
include 'filename.php';
require 'filename.php';
The difference between them is how they handle errors:
include
will emit a warning (E_WARNING) and the script will continue if the file is not found.require
will emit a fatal error (E_COMPILE_ERROR) and stop the script if the file is not found.There are also include_once
and require_once
, which are similar to include
and require
, respectively, but if the code from a file has already been included, it will not be included again.
Here's a basic example of how you might use these statements. Imagine you have a file named header.php
that contains the HTML header of your page, and you want to include it in all your pages.
header.php:
<!DOCTYPE html> <html> <head> <title>My Website</title> </head> <body>
index.php:
<?php include 'header.php'; ?> <h1>Welcome to my website!</h1> </body> </html>
In the above example, index.php
includes header.php
. When you access index.php
, it will also contain the content from header.php
. This is useful for elements like headers, footers, or reusable components that appear on multiple pages.
Remember, when using include
or require
, the path specified is relative to the file where the include
or require
statement is being used. If the file is not found at the specified path, PHP will also check in the directories specified in the include_path
configuration setting in the php.ini
file. If the file is not found there either, include
will emit a warning and require
will emit a fatal error.
How to use include and require in PHP:
include
and require
statements to include external files.<?php // Using include include 'header.php'; // Using require require 'footer.php';
Including external files with include
in PHP:
include
.<?php include 'functions.php'; // Rest of the code
Requiring essential files using require
in PHP:
require
statement.<?php require 'config.php'; // Rest of the code
Differences between include
and require
in PHP:
include
and require
.<?php // Using include include 'file1.php'; // If file not found, script continues with a warning // Using require require 'file2.php'; // If file not found, script terminates with a fatal error
Using include_once
and require_once
in PHP:
include_once
and require_once
to include files only once.<?php include_once 'config.php'; // Include only once require_once 'functions.php'; // Require only once // Rest of the code
Dynamic file inclusion with variables in PHP:
<?php $page = 'about'; include $page . '.php'; // Includes about.php dynamically
Error handling with include
and require
statements in PHP:
<?php // Using include with error handling if (file_exists('header.php')) { include 'header.php'; } else { die('Header file not found!'); } // Using require with error handling if (file_exists('footer.php')) { require 'footer.php'; } else { die('Footer file not found!'); }