How can the use of relative paths be optimized and made less error-prone when including resources in PHP-generated HTML pages?
When including resources in PHP-generated HTML pages, the use of relative paths can be optimized and made less error-prone by using the `__DIR__` magic constant to ensure that the paths are always relative to the current file's directory. This helps avoid issues when the current working directory changes. By using `__DIR__` in combination with concatenating the relative path to the resource, you can create a more robust and reliable way to include resources in PHP-generated HTML pages.
<?php
// Define the base path for resources
$basePath = __DIR__;
// Include CSS file using a relative path
echo '<link rel="stylesheet" href="' . $basePath . '/css/styles.css">';
// Include JavaScript file using a relative path
echo '<script src="' . $basePath . '/js/script.js"></script>';
?>