What are the best practices for structuring PHP code to avoid layout issues?

Layout issues in PHP code can be avoided by separating the logic from the presentation layer. One way to achieve this is by using a template engine like Twig or Blade, which allows you to keep your PHP code separate from the HTML markup. Another best practice is to use CSS for styling instead of mixing it with PHP code. By following these practices, you can ensure cleaner and more maintainable code.

// Example of separating logic from presentation using Twig template engine

// index.php
require_once 'vendor/autoload.php';

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

$products = ['Product 1', 'Product 2', 'Product 3'];

echo $twig->render('products.html', ['products' => $products]);

// templates/products.html
<ul>
  {% for product in products %}
    <li>{{ product }}</li>
  {% endfor %}
</ul>