What best practices should be followed when creating links dynamically based on the current URL in PHP?

When creating links dynamically based on the current URL in PHP, it's important to ensure that the links are generated correctly to avoid broken links or incorrect URLs. One best practice is to use the PHP $_SERVER['REQUEST_URI'] variable to get the current URL and then append any additional parameters or paths as needed to create the dynamic links.

$currentURL = $_SERVER['REQUEST_URI'];
$link = 'https://' . $_SERVER['HTTP_HOST'] . $currentURL . '/new-page.php';
echo '<a href="' . $link . '">New Page</a>';