What are the recommended methods for encoding URLs in PHP to prevent issues like &amamp; from occurring?

When encoding URLs in PHP, it's important to use the `urlencode()` function to properly encode special characters like `&`, `?`, and `=`. This helps prevent issues like `&` from occurring, which can lead to incorrect URLs being generated or processed. By using `urlencode()`, you ensure that the URL is properly formatted and can be safely used in web applications.

$url = 'https://example.com/page.php?param1=value1&param2=value2';
$encodedUrl = urlencode($url);

echo $encodedUrl;