Skip to main content

PHP Include Files


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"); ?>
  2. or
  3. <?php require("filename"); ?>

PHP Include Example

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

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

Note: Due to the size or complexity of this submission, the author has submitted it as a .zip file to shorten your download time. After downloading it, you will need a program like Winzip to decompress it.

Virus note: All files are scanned once-a-day by SourceCodester.com for viruses, but new viruses come out every day, so no prevention program can catch 100% of them.

FOR YOUR OWN SAFETY, PLEASE:

1. Re-scan downloaded files using your personal virus checker before using it.
2. NEVER, EVER run compiled files (.exe's, .ocx's, .dll's etc.)--only run source code.

Add new comment

CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.