How can PHP arrays be effectively used to manage multiple pages in a website layout?
When managing multiple pages in a website layout, PHP arrays can be effectively used to store information about each page such as its title, content, and other relevant details. By creating an array with key-value pairs for each page, you can easily access and display the necessary information dynamically based on the current page being viewed.
<?php
// Define an array to store information about each page
$pages = array(
"home" => array(
"title" => "Home Page",
"content" => "Welcome to our website!",
),
"about" => array(
"title" => "About Us",
"content" => "Learn more about our company.",
),
"contact" => array(
"title" => "Contact Us",
"content" => "Get in touch with us.",
),
);
// Get the current page from the URL parameter
$current_page = isset($_GET['page']) ? $_GET['page'] : 'home';
// Display the title and content of the current page
echo "<h1>{$pages[$current_page]['title']}</h1>";
echo "<p>{$pages[$current_page]['content']}</p>";
?>
Related Questions
- What role does the web server play in the execution of PHP scripts compared to opening them in a browser like Firefox?
- How can missing values in the array be replaced with static entries in the option list?
- What considerations should be made when restructuring the file inclusion concept in PHP to avoid link display issues?