DEV Community

It's Just Nifty
It's Just Nifty

Posted on • Originally published at niftylittleme.com on

Creating Reusable HTML Components In PHP

Unsplash Image By Ben Griffiths

Image Source

If you’re creating your website with PHP and HTML, you’ll want to reuse some components. For example, instead of creating identical headers for all the pages, you can create one header component and use it on multiple pages. Reusable PHP components will save you time.

In PHP web development, you can create these php components by creating a new file (example: header.component.php). Creating a folder for the file to go in is optional, but in this article, we will do it anyway (components/header.component.php).

Inside the header.component.php file, add the code for your header:

<?php
function createHeader() {
    return '
          <header>
       ...
          </header>
    ';
  }  
?>
Enter fullscreen mode Exit fullscreen mode

Then add this to your page’s code (Not the component. Example: index.php ):

        <?php
            include "./components/header.component.php";
            echo createHeader();
        ?>
Enter fullscreen mode Exit fullscreen mode

Happy Coding Folks!

AWS GenAI LIVE image

How is generative AI increasing efficiency?

Join AWS GenAI LIVE! to find out how gen AI is reshaping productivity, streamlining processes, and driving innovation.

Learn more

Top comments (0)

AWS Security LIVE!

Join us for AWS Security LIVE!

Discover the future of cloud security. Tune in live for trends, tips, and solutions from AWS and AWS Partners.

Learn More