Hello, welcome to zhullyblog. If this is your first time here , please
Subscribe to Zhullyblog by Email to get latest tutorial.
PHP Include and Require Files.
The best way to learn to code is to practice yourself. I urge you to practice with yourself
The PHP Include and Require file is similar to
links in HTML. The
include and
require file is used to link another page in a particular page.
The
include () and
require () statement allows you to include a PHP code of another file in another file.
The
include () and
require () statement saves you from typing all over but instead allow you to access the code in another file in a particular file.
A good example is including the header , footer , menu and other pages content.
Syntax of include and require
include 'filename'
or
require 'filename'
The slight difference between include and HTML links is that the link only works when you click on it directly but the include and require will show the content directly.
Let's take an example.
In the example below, we would include a header , footer files on the main page.
<html>
<head>
<title>How to include file </title>
</head>
<body>
<?php include "header.php"; ?>
<?php include "Blog.php"; ?>
<p>Text here </p>
<p>Text here</p>
<p>Text here </p>
<p>Text here</p>
<?php include "footer.php"; ?>
</body>
</html>
This means that you would have initially created a file saved as
header.php separately then include it in the file.
PHP require file
Here is an example
<html>
<head>
<title>How to require file </title>
</head>
<body>
<?php require "header.php"; ?>
<?php require "Blog.php"; ?>
<p>Text here </p>
<p>Text here</p>
<p>Text here </p>
<p>Text here</p>
</body>
</html>
Difference between Include and Require statement
There are solid difference between the include and require statement. Well, the
require() statement works like the
include () statement but the only difference is that :
• The
include () statement will generate a PHP warning once the file included can't be found
but
require ( ) will stop the script execution once the file can't be found.
This means that the file must have been created before you include or require it in a page.
But do you know that you MIGHT accidentally include a file twice? Ooh yes!! That will lead us to the include_once and require_once statement.
The include_once and require_once statement
There are times you accidentally include the same file twice in your code and seriously you have no idea that you have included the file and a problem popup.
What exactly do you do in this situation.
To resolve any conflict that may pop up, you use
include_once and
require_once statement.
This is how it works -
The
include_once and
require_once will include the file ONCE and PHP Include and Require Files.
Here is an example
<html>
<head>
<title>How to include file </title>
</head>
<body>
<?php include_once "header.php"; ?>
<?php require_once "Blog.php"; ?>
<p>Text here </p>
<p>Text here</p>
<p>Text here </p>
<p>Text here</p>
<?php include_once "footer.php"; ?>
</body>
</html>
The point is - include_once and require_once works better than the include ( ) and require () because
everyone is prone to mistake of repetition.
Take home: Create a file and name it header, make the header file your menu page, then include the header file in your main page.
Please share
Follow us on
1) Introduction to PHP
2) PHP Syntax
3) PHP Comment
4) PHP Data types
5) PHP Variables
6) PHP Echo and Print - How to display output
7) PHP If, if else and if elseif statement
8) PHP Lopps - While , Do while
9) PHP Strings : How to create and manipulate Strings
10) PHP Arrays
11)PHP FORMS: How to create a form, access data submitted in the form by user
Comments
Post a Comment