What are the best practices for using fragment identifiers in PHP to navigate to specific sections within a page?

When using fragment identifiers in PHP to navigate to specific sections within a page, it is important to ensure that the target element has a corresponding id attribute in the HTML markup. This id attribute should match the fragment identifier being used in the URL. By doing so, the browser will automatically scroll to the specified section when the URL is loaded.

<?php
// Get the fragment identifier from the URL
$fragment = $_SERVER['REQUEST_URI'];

// Check if the fragment identifier is not empty
if (!empty($fragment)) {
    // Output a script tag to scroll to the specified section
    echo '<script>document.getElementById("' . $fragment . '").scrollIntoView();</script>';
}
?>