How should URLs be encoded in HTML attributes in PHP?

When including URLs in HTML attributes in PHP, it is important to properly encode the URLs to ensure they are valid and do not cause any issues. This can be done using the `htmlspecialchars()` function in PHP, which will encode special characters in the URL to their corresponding HTML entities. This helps prevent any potential security vulnerabilities or syntax errors in the HTML code.

$url = "https://www.example.com/page?param1=value1&param2=value2";
$encoded_url = htmlspecialchars($url, ENT_QUOTES, 'UTF-8');
echo "<a href='$encoded_url'>Link</a>";