All TalkersCode Topics

Follow TalkersCode On Social Media

devloprr.com - A Social Media Network for developers Join Now ➔

How To Include A File In PHP

Last Updated : Mar 11, 2024

How To Include A File In PHP

In this tutorial we will show you the solution of how to include a file in PHP, here file is known as php file. So, in this article we understand how to include a php file in another php file.

Let us see below.

Step By Step Guide On How To Include A File In PHP :-

Here, first there is one requirement that is there must be 2 php files presented to include them.

This type of linking is used when you have a big project and for example the header and footer remain same in every page.

Hence, we create different files of header and footer and include them in file where we need them.

For this we are going to use two methods. First one is by using include() function and another is with the help of require() function.

<!DOCTYPE html>
<html>
<body>
 <?php include 'header.php';?>
<?php
 //rest of php code is here
?>
<?php include 'footer.php';?>
</body>
</html>
<!DOCTYPE html>
<html>
<body>
 <?php require 'header.php';?>
<?php
 //rest of php code is here
?>
<?php require 'footer.php';?>
</body>
</html>
  1. First, we write <! DOCTYPE html> which we used as an instruction to the web browser about what version of HTML file is written in.
  2. Secondly, the <html> tag is used to indicate the beginning of an HTML document.
  3. As above now <head> tag is used to contain information about web page. In this tag a <title> tag is used which helps us to specify a webpage title. Both <head> and <title> tags are Paired tags. So, both have </head> and </title> ending tags respectively.
  4. Here, then we create a body tag. All the content which we want to show on browser’s screen or display is always written inside this codes.
  5. As here you see that we provide you two example with two different methods.
  6. In first method we use include () function whereas in next we use require() function. One thing to note here that the job of both function is same then what is the difference between them that they have two different name.
  7. The difference between them is that if we use include function and the file which we want to include does not found due to some error or typing mistake. Then our rest code is executed and an error is displayed where we include file.
  8. Whereas in case of require function, error is again showed here if the file is missing or due to some typing mistake. But the main thing is that the rest code did not work. If required found error then it will only show that error and does not show anything. So, we hope that you understand both the codes easily.
  9. At last, the <body> and <html> tags are closed with </body> and </html> respectively.

Conclusion :-

At last in conclusion, here we can say that with the help of this article we are able to understand how to include a file in php.

I hope this tutorial on how to include a file in PHP helps you and the steps and method mentioned above are easy to follow and implement.