What are the potential pitfalls of mixing PHP and HTML code within the same file, and how can they be avoided?
Mixing PHP and HTML code within the same file can lead to messy and hard-to-maintain code. To avoid this, it's recommended to separate PHP logic from HTML presentation by using a templating engine like Twig or Blade. This helps in keeping code organized and improves readability.
<?php
// Example of using a templating engine like Twig to separate PHP logic from HTML presentation
require_once 'vendor/autoload.php';
$loader = new \Twig\Loader\FilesystemLoader('templates');
$twig = new \Twig\Environment($loader);
$template = $twig->load('index.html');
$data = [
'title' => 'Welcome to my website',
'content' => 'This is some content for the homepage',
];
echo $template->render($data);
?>