The include() function is used to put data from one PHP file to another. PHP file.
When you create a dynamic website, you remove the same code from the pages and create another PHP file for include.
We can get new file content on another page using include() function.
To include content from a different PHP file, we use the include function and require() function in PHP.
A simple static website contains the header, footer, sidebar content.
Here, we can create other files.
For example -
We remove the header code from the index file and create another file header.php and same remove sidebar, footer and create another PHP files - sidebar.php, footer.php.
We put the content into the index file from the header, sidebar and footer file.
Now you do not need to change the header on every page of the website. Just edit the header file and change the content. The header change reflects on all pages of the website.
You have to follow the same process for sidebar and footer.
The header, sidebar, and footer have the same content for all pages. That's why we use the include() function.
Include() function in PHP
The include function is used to get all the content from the included file. If you want to put another file content on a page, so you can use the include() function .
You have to pass the file name (ex. about.php) inside the include() function.
If there is any problem in loading a file, then the include() function generates a warning but the script will continue execution.
Syntax
include("file_name.php");
In this syntax, we include a file inside the include() function. Use the file name and extention for include .
Example of Include() function
Suppose that we have 3 pages on the website. The three pages have the same content for header, sidebar, footer.
Now if you have to make some changes in the header then you will edit the three pages one by one. If you have 50 pages on your website, then you have to open 50 pages again and change the header.
It will take a lot of time and complexity.
We can solve this problem using the header() function.
1. Remove all codes of header and paste in the new file and save as header.php.
2. Remove all codes of the sidebar and paste in the new file and save as sidebar.php.
3. Remove all codes of the footer and paste in the new file and save as footer.php.
Let's implement an example.
Suppose that a website has three pages.
1. index.php
2. about.php
4. service.php
Before using include() function
1.The index page contains the content.
<html>
Header
index body content
Sidebar
footer
</html>
2. The about page contains the content.
<html>
Header
about main body content
Sidebar
footer
</html>
3.The service page contains the content .
<html>
Header
Service main body content
Sidebar
footer
</html>
Use include() function
First of all, remove the same content from all these three pages and create new files according to content behave.
1. Cut all header code and paste in new file and save as header.php .
Header
2. Cut all sidebar code and paste in the new file and save as sidebar.php .
Sidebar
3. Cut all footer code and paste in the new file and save as footer.php .
footer
Now include header, sidebar and footer file in -
1. Index.php
2. about.php
3.service.php
1.Put all content in index page from the files header, sidebar and footer using include() function.
<html>
<?php include("header.php"); ?>
index body content
<?php include("sidebar.php"); ?>
<?php include("footer.php"); ?>
</html>
2. Put all content in about page from the files header, sidebar and footer using include() function.
<html>
<?php include("header.php"); ?>
about main body content
<?php include("sidebar.php"); ?>
<?php include("footer.php"); ?>
</html>
3. Put all content in service pages from the file header, sidebar and footer using include() function.
<html>
<?php include("header.php"); ?>
Service main body content
<?php include("sidebar.php"); ?>
<?php include("footer.php"); ?>
</html>
A website can contain a lot of pages. Now you can change header content from header.php without editing all pages of the website. Just change in header.php file and it reflects all pages of the website. Follow the same for sidebar and footer.
Recommended Posts:-