What are the potential pitfalls of not encoding URLs correctly in HTML attributes in PHP?

If URLs are not encoded correctly in HTML attributes in PHP, it can lead to invalid URLs being generated, which can cause issues such as broken links or security vulnerabilities. To avoid this problem, it is important to properly encode URLs using functions like `urlencode()` or `htmlspecialchars()` before inserting them into HTML attributes.

$url = "https://www.example.com/page?param=value";
$encodedUrl = htmlspecialchars($url, ENT_QUOTES, 'UTF-8');
echo "<a href='$encodedUrl'>Link</a>";