In the provided PHP script, what improvements can be made to enhance readability and maintainability, such as separating HTML output from PHP logic?
The issue with the provided PHP script is that it mixes HTML output with PHP logic, which can make the code harder to read and maintain. To improve readability and maintainability, it's recommended to separate the HTML output from the PHP logic by using a templating system or breaking the code into smaller, more focused functions.
<?php
// Separate HTML output from PHP logic using a templating system or functions
function generateHTML($name, $age) {
return "<p>Name: $name</p><p>Age: $age</p>";
}
$name = "John Doe";
$age = 30;
$htmlOutput = generateHTML($name, $age);
echo $htmlOutput;
?>