How can PHP be effectively used as a template engine to render different page content based on user requests?
To effectively use PHP as a template engine to render different page content based on user requests, you can create separate template files for each type of content and use PHP to include the appropriate template based on the user's request. This can be achieved by passing a parameter in the URL or using a session variable to determine the content to be displayed.
<?php
// Check the user's request
$page = isset($_GET['page']) ? $_GET['page'] : 'home';
// Include the appropriate template based on the user's request
switch ($page) {
case 'home':
include 'templates/home.php';
break;
case 'about':
include 'templates/about.php';
break;
case 'contact':
include 'templates/contact.php';
break;
default:
include 'templates/error.php';
break;
}
?>
Keywords
Related Questions
- What is the difference between using POST and REQUEST methods in PHP form handling?
- Are there any best practices or resources for handling time-based logic in PHP to ensure accurate and efficient content display on a website?
- What are the potential pitfalls of using the __call() magic method in PHP for method invocation?