What are the potential security risks associated with embedding PHP code directly into HTML files?

Embedding PHP code directly into HTML files can pose security risks such as code injection attacks, exposing sensitive information, and making it easier for malicious users to exploit vulnerabilities. To mitigate these risks, it is recommended to separate PHP logic from HTML markup by using a template engine like Twig or Smarty, or by implementing a proper input validation and output escaping.

<?php
// Example of separating PHP logic from HTML markup using a template engine like Twig

require_once 'vendor/autoload.php';

$loader = new \Twig\Loader\FilesystemLoader('templates');
$twig = new \Twig\Environment($loader);

$data = [
    'title' => 'Welcome to our website',
    'content' => 'This is a sample content',
];

echo $twig->render('index.html', $data);
?>