What is the difference between urlencode and rawurlencode in PHP and when should each be used?

urlencode and rawurlencode are both PHP functions used to encode a string for use in a URL. The main difference between them is how they handle special characters. urlencode encodes spaces as plus signs (+) and special characters as % followed by their ASCII value. rawurlencode encodes spaces as %20 and special characters as % followed by their hexadecimal value. Use urlencode when you want to encode a string for use in a query string or form data, as it is more user-friendly and easier to read. Use rawurlencode when you want to encode a string for use in a URL path or when you need to be more strict about encoding special characters.

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

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