How can PHP be used to dynamically load different pages based on URL parameters?
To dynamically load different pages based on URL parameters in PHP, you can use the $_GET superglobal array to retrieve the parameter value from the URL and then include the corresponding page based on that value. This allows you to create a single PHP script that can handle multiple pages based on the URL parameters.
<?php
$page = isset($_GET['page']) ? $_GET['page'] : 'home';
switch ($page) {
case 'about':
include 'about.php';
break;
case 'contact':
include 'contact.php';
break;
default:
include 'home.php';
break;
}
?>
Related Questions
- How can PHP developers efficiently handle file uploads with unique timestamps appended to the file names to prevent duplicates?
- What is the difference between HTML entities and URL encoding in PHP and when should each be used?
- What are some best practices for handling CSV files with line breaks within data entries in PHP?