What are the best practices for encoding and decoding data passed through URLs in PHP to prevent errors or security vulnerabilities?

When passing data through URLs in PHP, it is important to properly encode and decode the data to prevent errors or security vulnerabilities. One common method to achieve this is by using the urlencode() function to encode data before passing it in the URL, and then using urldecode() function to decode the data when retrieving it.

// Encoding data before passing it in the URL
$data = "Hello, World!";
$encoded_data = urlencode($data);

// Decoding data when retrieving it from the URL
$decoded_data = urldecode($_GET['data']);