Backend Development 4 min read

How to Use PHP's urldecode() Function for URL Decoding

This article explains the concept of URL encoding/decoding, introduces PHP's urldecode() function with its signature, provides step‑by‑step example code showing how to encode a string with urlencode() and then decode it back with urldecode(), and notes its limitations and the alternative rawurldecode() function.

php中文网 Courses
php中文网 Courses
php中文网 Courses
How to Use PHP's urldecode() Function for URL Decoding

When developing web applications you often need to encode and decode URLs; PHP offers built‑in functions for this purpose, one of which is urldecode() . This article introduces the usage and example code of the urldecode() function.

First, understand that URLs cannot contain certain special characters directly (such as spaces, slashes, question marks). URL encoding converts these characters into a specific string representation, while URL decoding reverses the process to restore the original characters.

The urldecode() function is used to decode a URL‑encoded string. It takes a single parameter – the encoded string – and returns the decoded original string. Its prototype is:

string urldecode ( string $str )

The function accepts only one argument and returns the decoded string.

Below is a practical example. Suppose we have a URL parameter that needs decoding. First we encode the parameter using urlencode() :

$param = "hello world";
$urlParam = urlencode($param);

The variable $urlParam now holds the value "hello%20world". Next we decode this encoded parameter with urldecode() :

$decodedParam = urldecode($urlParam);
echo $decodedParam;

Running the code outputs "hello world", demonstrating that urldecode() successfully converts the encoded URL parameter back to its original string.

It is important to note that urldecode() cannot properly decode non‑printable characters in the range %00 to %20 or non‑ASCII characters below %7F. For such cases, the rawurldecode() function should be used instead.

In summary, urldecode() is a useful PHP function for URL decoding, allowing developers to easily revert URL‑encoded strings to their original form.

Backendphp-functionsURL decodingurldecode
php中文网 Courses
Written by

php中文网 Courses

php中文网's platform for the latest courses and technical articles, helping PHP learners advance quickly.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.