What are the best practices for loading specific scripts only when certain pages are accessed in PHP?

When working with PHP, it's important to load specific scripts only when certain pages are accessed to optimize performance and reduce unnecessary resource consumption. One way to achieve this is by using conditional statements to check the current page being accessed and then include the necessary scripts based on that condition.

<?php
// Get the current page URI
$current_page = $_SERVER['REQUEST_URI'];

// Check if the current page is 'example.php' and load specific scripts
if ($current_page == '/example.php') {
    echo '<script src="example.js"></script>';
}
?>