How can htmlentities and urldecode functions be used to handle URL encoding in PHP?

When dealing with URL encoding in PHP, htmlentities and urldecode functions can be used to handle special characters in URLs. htmlentities function converts special characters to their HTML entities, making them safe to be included in URLs. urldecode function decodes any URL-encoded string, converting %XX sequences back to their original characters.

// URL encoding using htmlentities and urldecode functions
$url = "https://www.example.com/?name=" . htmlentities("John Doe");
$encoded_url = urlencode($url);

// Decoding the URL
$decoded_url = urldecode($encoded_url);

echo $url . "<br>";
echo $encoded_url . "<br>";
echo $decoded_url;