If you want to save your time and effort of writing the same code many times. You have to use the PHP include statement to include the file code without writing it again and again.
What is PHP Include Statement?
PHP include statement is the way of including the file code inside another file. By including the file into another file, you can add scripting codes in the required location of the file. It copies the included file code in the added location of the file which is same as writing the scripting code in the location.
Syntax
1 2 3 |
<?php include("path of the file with filename"); ?> |
OR you can use the below syntax
1 2 3 |
<?php include "path of the file with filename"; ?> |
The first syntax contains the parenthesis while the second syntax is without the parenthesis.
You don’t need to type the same code many times in the required location of the file. You have to just type and save the code in the PHP file. Next time when you have to use the code into another file, you have to just use the PHP include statement to add the required script.
How to Include File into Another File Using PHP Include
If you are writing code for your project based on PHP application. You may require to include the same piece of code many times in multiple files.
The body part of the application can contain different codes. While header, footer and menu may contains the same codes. You can reduce your time of writing same codes for header, footer and menu by using the below syntax and examples.
Examples1
Suppose that, you have to include the file with name ‘menu.php’ which contains the below code to create a menu.
1 2 3 4 5 6 7 8 9 10 11 12 |
<style> .myrealmenu{ background:#ccc; } </style> <ul class="myrealmenu"> <li><a href="home.php">Home</a></li> <li><a href="about.php">About</a></li> <li><a href="gallery.php">Gallery</a></li> <li><a href="projects.php">Projects</a></li> <li><a href="contact.php">Contact</a></li> </ul> |
You can include the above files as given in the example below.
1 2 3 4 5 |
<?php include("menu.php"); echo "Welcome to our website! Here, you will learn with lots of examples."; ?> <p>This page contains PHP codes.</p> |
Output
Welcome to our website! Here, you will learn with lots of examples.
This page contains PHP codes.
Examples2
Suppose that, you have to include the file with name ‘header.php’ which contains the below code to create a header.
1 2 3 4 5 6 7 |
<style> .myrealheader{ background: #74b5f7; padding: 15px; } </style> <header class="myrealheader"><strong>Tutorialdeep</strong></header> |
You can include the above files as given in the example below.
1 2 3 4 5 |
<?php include("header.php"); echo "Welcome to Tutorialdeep! Learn the basics of PHP with examples."; ?> <p>Add your PHP codes here.</p> |
Output
Welcome to Tutorialdeep! Learn the basics of PHP with examples.
Add your PHP codes here.
Use Include_once Statement
Suppose you have a file name menu.php to include into another file. Mistakenly, you have included the file for more than one time. If you are using PHP include statement to include the file into the another file. This may give some error when you execute the code.
In that condition, you have to use the PHP include_once statement which include the file for only once. If you include the same file for more than one time by using the PHP include_once statement. It will not include the file for second time if already included for once.
Syntax
1 2 3 |
<?php include_once("path of the file with filename"); ?> |
OR you can use the below syntax
1 2 3 |
<?php include_once "path of the file with filename"; ?> |
See the below examples to learn the use with live demo.
Example3
Suppose you have a file name ‘footer.php’ as given below.
1 2 3 4 5 6 7 |
<style> .myrealfooter{ background: #74b5f7; padding: 15px; } </style> <footer class="myrealfooter">Copyright © 2019 Tutorialdeep</footer> |
If you include the above file for more than one times using PHP include_once statement as given below.
1 2 3 4 5 6 7 8 |
<?php echo "Welcome to our website Tutorialdeep! Use examples on your project."; ?> <p>This is your PHP code.</p> <?php include_once("footer.php"); include_once("footer.php"); ?> |
Output
Welcome to our website Tutorialdeep! Use examples on your project.
Add your PHP codes here.
You may also like to read
- What is PHP Echo Statement
- How PHP explode Function Convert String Into an Array
- PHP implode Function Join Array Element With String
- Interview Question: PHP Programs to Print Pyramid Star Patterns
- Ajax Image Upload Using PHP, jQuery With Image Preview
Hope, you like this post of how to use the PHP include statement. If you have any query regarding the tutorial, please comment below.
Also tell me, how you have included your file into another file using PHP. Please leave a comment below.