What are some best practices for handling URL generation and linking in PHP scripts?

When generating URLs and linking in PHP scripts, it is important to use proper escaping and encoding to ensure that the URLs are valid and secure. One common practice is to use the `urlencode()` function to encode any dynamic data that is included in the URL. Additionally, it is recommended to use relative URLs whenever possible to ensure portability and avoid hardcoding domain names.

// Example of generating a URL with dynamic data using urlencode()
$id = 123;
$name = "John Doe";

$url = "http://example.com/profile.php?id=" . urlencode($id) . "&name=" . urlencode($name);

echo "<a href='" . $url . "'>View Profile</a>";