How can PHP be used to display a specific page within a website layout?
To display a specific page within a website layout using PHP, you can use a combination of PHP includes and conditional statements. By creating a separate PHP file for each page content and then including it in the main layout file based on a parameter or variable, you can dynamically display different pages within the same layout structure.
<?php
$page = isset($_GET['page']) ? $_GET['page'] : 'home'; // Get the page parameter from the URL, default to 'home' if not set
if ($page == 'home') {
include 'pages/home.php'; // Include the home page content
} elseif ($page == 'about') {
include 'pages/about.php'; // Include the about page content
} elseif ($page == 'contact') {
include 'pages/contact.php'; // Include the contact page content
} else {
include 'pages/404.php'; // Include a 404 page if the specified page is not found
}
?>
Keywords
Related Questions
- How can PHP developers prevent Cross-Site Scripting (XSS) attacks when outputting user input in HTML or URLs?
- What are the advantages of using a Mailer class like PHPMailer or SwiftMailer over the mail() function in PHP?
- What are some best practices for obtaining a functioning version of PHPBB or other forum software for a website?