How can PHP be used to correctly generate and handle URLs with UTF-8 encoded special characters for use in HTML output?

When generating URLs with UTF-8 encoded special characters in PHP for use in HTML output, it is important to properly encode the characters to ensure they are displayed correctly in the browser. The urlencode() function can be used to encode special characters in the URL, while html_entity_decode() can be used to decode them for display in HTML.

// Example of generating and handling URLs with UTF-8 encoded special characters
$url = 'https://example.com/?query=' . urlencode('Café');

// Output the URL in HTML
echo '<a href="' . $url . '">' . html_entity_decode('Café') . '</a>';