When we are going to pass data through a URL, we must encode it some way, as the HTTP standards dictate that only certain characters are allowed to exist in a URL. The 'url_encode()' function provides this functionality for us. It will convert the majority of non-alphanumeric characters into a percent sign followed by the two digit hex code for that character. Spaces will be converted to a plus sign.
It is important to encode data being passed through the URL so that browsers will not mangle it. Without encoding, the integrity of any data passed can not be trusted. Encoding data with the 'url_encode()' function is as simple as passing it the data we want encoded. Let's see an example.
<?php $mystring = 'Here is some data!'; $encoded = urlencode ($mystring); ?>
Now, that would produce the following output:
In order to decode the data, we would pass it to the 'url_decode()' function. It is good to note, however, that data passed to a PHP script from the URL does not need to be decoded. PHP handles this automatically for us.