What are the potential pitfalls of trying to run PHP scripts within template files?

Running PHP scripts within template files can lead to mixing logic with presentation, making the code harder to maintain and debug. It can also increase the risk of security vulnerabilities if proper input validation and output escaping are not implemented. To avoid these pitfalls, it's recommended to separate logic from presentation by using a template engine like Twig or Smarty.

// Example of separating logic from presentation using Twig template engine
$loader = new \Twig\Loader\FilesystemLoader('/path/to/templates');
$twig = new \Twig\Environment($loader);

// Render the template with data
echo $twig->render('template.twig', ['variable' => $value]);