Skip to main content

PHP Include Files

Screenshot
Body

In this tutorial, we are going to learn about PHP Include Files. This is a useful idea, you can able to include a PHP file to another PHP script or into your HTML source code on multiple pages of your system or website. For short, this function analyze and work in a specific file. The include() functions grant you to do this.

PHP Include Files can be used multiple times and single time.

Example

Multiple Times
<?php include("footer.php"); ?>

Single Time
<?php include_once("footer.php"); ?>

We have two PHP functions that we can use to include one PHP file to another PHP file.

  • The include() Function
  • The require() Function

Syntax

  1. <?php include("filename"); ?><br />
  2. or<br />
  3. <?php require("filename"); ?>

PHP Include Example

We create a simple footer. Save as "footer.php".

  1. <?php<br />
  2. echo "<p style='font-size:18px; text-align:center;'>Copyright &copy; ". date('Y') .", Free source code, tutorials and articles</p>";<br />
  3. ?>

To include this in the footer section in our page, use the include function.

Then, create simple page. Save as "index.php".

  1. <!DOCTYPE html><br />
  2. <html><br />
  3. <head><br />
  4. <title><br />
  5. PHP Include File<br />
  6. </title></p>
  7. <p><style type="text/css"><br />
  8. .blink_text {<br />
  9. -webkit-animation-name: blinker;<br />
  10. -webkit-animation-duration: 1s;<br />
  11. -webkit-animation-timing-function: linear;<br />
  12. -webkit-animation-iteration-count: infinite;</p>
  13. <p>-moz-animation-name: blinker;<br />
  14. -moz-animation-duration: 1s;<br />
  15. -moz-animation-timing-function: linear;<br />
  16. -moz-animation-iteration-count: infinite;</p>
  17. <p> animation-name: blinker;<br />
  18. animation-duration: 1s;<br />
  19. animation-timing-function: linear;<br />
  20. animation-iteration-count: infinite;</p>
  21. <p> color:black;<br />
  22. }</p>
  23. <p>@-moz-keyframes blinker {<br />
  24. 0% { opacity: 1.0; }<br />
  25. 50% { opacity: 0.0; }<br />
  26. 100% { opacity: 1.0; }<br />
  27. }</p>
  28. <p>@-webkit-keyframes blinker {<br />
  29. 0% { opacity: 1.0; }<br />
  30. 50% { opacity: 0.0; }<br />
  31. 100% { opacity: 1.0; }<br />
  32. }</p>
  33. <p>@keyframes blinker {<br />
  34. 0% { opacity: 1.0; }<br />
  35. 50% { opacity: 0.0; }<br />
  36. 100% { opacity: 1.0; }<br />
  37. }<br />
  38. </style><br />
  39. </head></p>
  40. <p><body><br />
  41. <center><br />
  42. <h1><br />
  43. Welcome!!!<br />
  44. <a href="http://www.sourcecodester.com/" style="text-decoration:none;"><br />
  45. <b class="blink_text" style="color:blue;"><br />
  46. To Sourcecodester.com<br />
  47. </b><br />
  48. </a><br />
  49. </h1><br />
  50. <p style="color:black; font-size:18px;"><br />
  51. Do you have source code, articles, tutorials, web links, <br /><br />
  52. and books to share? You can write your own content here. <br /><br />
  53. You can even have your own blog.<br />
  54. </p><br />
  55. </center><br />
  56. <?php include('footer.php'); ?><br />
  57. </body></p>
  58. <p></html>

And, this is the result of the code above.

result - include function

As of now this is the basic syntax of PHP Include File.

So what can you say about this work? Share your thoughts in the comment section below or email me at [email protected]. Practice Coding. Thank you.

Add new comment