What is the purpose of using rawurlencode in PHP for URLs?

When working with URLs in PHP, it is important to properly encode special characters to ensure that the URL is valid and can be safely transmitted. The rawurlencode function in PHP is used to encode characters in a URL string that are not allowed or have special meanings, such as spaces or punctuation marks. This function ensures that the URL is correctly formatted and can be safely used in web applications.

$url = "https://www.example.com/page.php?name=John Doe";
$encoded_url = rawurlencode($url);

echo $encoded_url;