PHP Include Files

Submitted by: 
Language: 
Visitors have accessed this post 1160 times.

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="<a href="https://www.sourcecodester.com/"">https://www.sourcecodester.com/"</a> 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.


Add new comment

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • You may insert videos with [video:URL]
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd> <table> <tr> <td> <th> <img> <h1> <h2> <h3> <iframe> [video]
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <asp>, <c>, <cpp>, <csharp>, <css>, <html4strict>, <java>, <javascript>, <mysql>, <php>, <python>, <sql>, <vb>, <vbnet>. The supported tag styles are: <foo>, [foo].
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.