Welcome
    

PHP Layout

 

In web development, a layout in PHP typically refers to the structure or template used to organize the visual elements of a web page. Layouts often include common components like headers, footers, navigation bars, and sidebars that remain consistent across multiple pages of a website.

 

 

One common approach to implementing layouts in PHP is by using template files and including them where necessary. Here's a simple example of how you can create a basic layout in PHP:

 

 

layout.php (Main Layout Template)

 

 

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title><?php echo $pageTitle; ?></title>
 
  <!-- Include CSS files -->
    <link rel="stylesheet" href="styles.css">
</head>
<body>

<!-- Include header -->
<?php include 'header.php'; ?>

<!-- Page content -->
<div class="content">
    <?php include $pageContent; ?>
</div>

<!-- Include footer -->
<?php include 'footer.php'; ?>

</body>
</html>

 

 

header.php (Header Template)

 

 

<header>
    <h1>My Website</h1>
    <nav>
        <ul>
            <li><a href="index.php">Home</a></li>
            <li><a href="about.php">About</a></li>
            <li><a href="contact.php">Contact</a></li>
        </ul>
    </nav>
</header>

 

 

footer.php (Footer Template)

 

 

<footer>
    <p>&copy; <?php echo date("Y"); ?> My Website</p>
</footer>

 

 

index.php (Main Page)

 

 

<?php
// Set page variables
$pageTitle = "Home";
$pageContent = "content/home.php";

// Include the main layout
include "layout.php";
?>

 

 

content/home.php (Content of Home Page)

 

 

<h2>Welcome to My Website!</h2>
<p>This is the home page content.</p>

 

 

In this example, layout.php represents the main layout template. It includes placeholders for the header, footer, and page content. The main page files like index.php set the page variables ($pageTitle and $pageContent) and then include the main layout file.

 

 

This approach allows you to create modular and reusable components for your website layout, making it easier to maintain and update. Additionally, you can use PHP variables to dynamically set page titles and include different content files based on the current page being viewed.