How can PHP be used to create a main layout page that can be applied to multiple subpages with different content?
To create a main layout page that can be applied to multiple subpages with different content, you can use PHP include or require functions to include the main layout file in each subpage. Within the main layout file, you can define placeholders for dynamic content that will be replaced by the specific content of each subpage. This allows you to maintain a consistent layout across multiple pages while easily updating the main layout in one central location.
// main_layout.php
<html>
<head>
<title>My Website</title>
</head>
<body>
<header>
<h1>Welcome to My Website</h1>
</header>
<main>
<?php echo $content; ?>
</main>
<footer>
<p>&copy; <?php echo date("Y"); ?> My Website</p>
</footer>
</body>
</html>
```
```php
// subpage1.php
<?php
$content = "<h2>Subpage 1 Content</h2>
<p>This is the content for Subpage 1.</p>";
require_once('main_layout.php');
?>
```
```php
// subpage2.php
<?php
$content = "<h2>Subpage 2 Content</h2>
<p>This is the content for Subpage 2.</p>";
require_once('main_layout.php');
?>
Keywords
Related Questions
- What are the potential pitfalls of inconsistent return values (null vs false) in PHP functions?
- What are the potential drawbacks of using JavaScript for star rating systems in PHP?
- What are the potential issues with dynamically generating HTML tables using PHP and trying to manipulate them with JavaScript?