What is the purpose of using templates in PHP and what are the benefits?
Using templates in PHP allows for the separation of presentation logic from business logic, making code more organized and maintainable. Templates help in creating reusable components for web pages, improving code readability and reducing duplication. They also make it easier to update the design of a website without having to modify the underlying PHP code.
// Example of using a template in PHP
// Template file (template.php)
<html>
<head>
<title><?php echo $title; ?></title>
</head>
<body>
<h1><?php echo $heading; ?></h1>
<p><?php echo $content; ?></p>
</body>
</html>
// PHP file (index.php)
<?php
$title = "Welcome to my website";
$heading = "Hello, world!";
$content = "This is a sample content.";
include 'template.php';
?>