PHP include statement

Sometimes we need to re-use same code in different PHP files such as Header file. We use same header file in different page. For that reason we need to write same code in different page. It is time consuming and not good way of writing PHP code. To solve this problem we use PHP include() statement.

PHP include() statement Structure

Now the question is how we use PHP include() statement in our code. We use include syntax to add same type of code. Suppose that, we want to add header.php file in register.php file. We write include("header.php") in the register.php file. So the structure of include() statement is

                        include("file name or file path")
                 include("header.php")   

 

PHP include statement example

Think that we have a file name title.php that show the title in header.php file. So we write the title.php file.
<?php
echo "PHP Tutorials";
?> 
And our header.php file is
<html>
<title>
<?php
include("title.php");
?>
</title>
Now we think that we add the header.php file in register.php file. So the code is
<?php
include("header.php");
?>
<form method="post" action="">
Username:<input type="">
Password: <input type="">
</form>
</html>
In that way we can use include statement in any place of the PHP code.

Important Note: One important question is if the include statement do not found the path or we do not add the path what will occur. The answer is nothing will occur, it just produce a warning message (E_WARNING) and your code will continue. So try to write a PHP code where the include statement missing the path. And then try to understand what will occur.  

Comments

Popular posts from this blog

How to construct a B+ tree with example

How to show only month and year fields in android Date-picker?

Visitor Counter Script Using PHP