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 the header() function in PHP be utilized to control caching behavior and prevent unauthorized access to pages?
- How can PDO be utilized to address the issue of accessing and querying multiple MDB files in PHP?
- What are the security implications of including external files in PHP forum scripts?