URL encoding, also known as "percentage encoding", is a
mechanism for encoding information in a single resource identifier (URI).
Although it is known as URL encoding, it is actually used more generally
within the core Single Resource Identifier (URI), which includes
both a single resource locator (URL) and a single resource name (URN).
Thus, it is also used when preparing data of the type
"application/x-www-form-urlencoded", as it is often used when
presenting HTML form data in HTTP requests.
What is URL decoding and why is it required?
URL decoding is the reverse process of URL encoding.It
is used to analyze query strings or path parameters
passed in the URL.It is also used to decode
HTML form parameters, which are presented in MIME format with
the application/XWW-FORM-URLENCODE
URLs, as you may know, can only contain a limited
set of characters from the US-ASCII character set.These characters include
alphabets (A-z a-z), numbers (0-9), hyphen (-), underscore (_), tilde (~) and
Any character outside this allowed set is encoded
using URL encoding or percentage encoding.
This is why it becomes necessary to decode the query strings
or path parameters passed to the URL in order to get the actual values.
A good example of where this might be needed. Let's
say you need to pass another url as a parameter in the url. You can't substitute this url directly, that's
where url encoding comes to the rescue.
// http%3A%2F%2Fexample.com%2Findex-2.php
$url = urlencode( 'http://example.com/index-2.php' );
// http://example.com/index.php?url=http%3A%2F%2Fexample.com%2Findex-2.php
echo 'http://example.com/index.php?url=' . $url;