Is it necessary to use both rawurlencode() and htmlspecialchars() when encoding URLs in PHP?

When encoding URLs in PHP, it is not necessary to use both rawurlencode() and htmlspecialchars(). rawurlencode() is used to encode special characters in a URL, while htmlspecialchars() is used to encode special characters in HTML. If you are generating URLs that will be used in HTML attributes or text, it is sufficient to use rawurlencode() to encode the URL.

$url = 'https://www.example.com/page?param=value';
$encodedUrl = rawurlencode($url);
echo '<a href="' . $encodedUrl . '">Link</a>';