PHP tutorial-file include


File include in PHP

PHP allows you to reuse your code by including the same code written in a file in many pages that need this code. For example, in many pages of your web site there some common contents that every page should have. These common contents can be header, footer, or menu. By writing the code of the header, footer, or menu in a separated file and include it in every page that need it, you will save a lot of work.
 
In PHP, you can include a separated file to a page by using include(filename) and require(filename) functions. The two functions are almost the same. The difference is that the include() function still continue to execute the code although there is an error in the code while the require() function stops executing the code if there is an error.

Example:
The menu contents of the PHPtutorial.php page are written in a file called phpmenu.inc:
<div 'menu' style='visibility:visible'>

<a href=' php_tutorial_lesson.htm'>Understanding PHP</a>
<a href=' php_first_program.htm'>Your PHP First Program</a>
<a href=' php_syntax.htm'>PHP Syntax</a>
<a href=' php_variable.htm'>PHP Variable</a>
<a href=' php_operators.htm'>PHP Operators</a>
<a href=' php_conditional_statements.htm'>PHP Conditional Statements</a>
<a href=' php_arrays.htm'>PHP Arrays</a>
<a href=' php_loops.htm'>PHP Loops</a>
<a href=' php_function.htm'>PHP Function</a>
<a href=' php_form.htm'>PHP Form and User Interactivity</a>
<a href=' php_include-files.htm'>PHP Inclusing files</a>
<a href=' php_string-manipluation-remove-spaces.htm'>PHP String Manipulation:<br>Remove spaces</a>
<a href=' php_string-manipluation-count-characters.htm'>PHP String Manipulation:<br>Count characters</a>
<a href=' php_string-manipluation-change-cases.htm'>PHP String Manipulation:<br>Change cases</a>
<a href='php_string-manipluation-compare.htm'>PHP String Manipulation:<br>Compare strings</a>
<a href='php_string-manipluation-split-join.htm'>PHP String Manipulation:<br>Split and Join strings</a>
<a href='php_string-manipluation-substring.htm'>PHP String Manipulation:<br>Get substrings</a>
<a href='php_string-manipluation-find-replace.htm'>PHP String Manipulation:<br>Find and Replace strings</a>
<a href='php_regular-expression.htm'>PHP Regular Expression</a>
<a href='php_regular-expression-pattern.htm'>PHP Regular Expression:<br>Writing patterns</a>
<a href='php_regular-expression-special-characters.htm'>PHP Regular Expression:<br>Special Characters</a>

<a href=' php_mysql_connection.htm'>PHP and Mysql databases Connection</a>
<a href=' php_session.htm'>PHP SESSION Variable</a>
<a href=' php_query-string.htm'>PHP Passing values across web pages</a>
<a href=' php_oop-introduction.htm'>PHP Introduction to OOP</a>
<a href=' php_oop-method-parameters.htm'>PHP OOP Passing parameters</a>
<a href=' php_oop-constructor.htm'>PHP OOP Constructor</a>
<a href=' php_oop-inheritance.htm'>PHP OOP Inheritance</a>

</div>

In the PHPtutorial.php file, you can include the phpmenu.inc file as shown below:
<html>
<head><title>PHP tutorial</title></head>
<body>
<?php include("phpmenu.inc");
//or require("phpmenu.inc");
?>
</body>
</html>


Comments

Dara comment

 Dara

Please see the answer on the forum page. http://www.worldbestlearningcenter.com/thread.php


2013-09-28
PHAN Neth comment

 PHAN Neth

Dear Teacher !
I'm PHAN Neth. I have a problem want you help me. I have three php files. There are index.php, baner.php, and menu.php. I want to include baner.php and menu.php to index.php by using include() function. But when i include the both of files to index.php it don't know the picture and css that i use in menu.php and baner.php, but if i run baner.php or menu.php on itself i it work with the picture and css.


2013-09-28




This website intents to provide free and high quality tutorials, examples, exercises and solutions, questions and answers of programming and scripting languages:
C, C++, C#, Java, VB.NET, Python, VBA,PHP & Mysql, SQL, JSP, ASP.NET,HTML, CSS, JQuery, JavaScript and other applications such as MS Excel, MS Access, and MS Word. However, we don't guarantee all things of the web are accurate. If you find any error, please report it then we will take actions to correct it as soon as possible.