Backend Development 5 min read

Using PHP urlencode() and urldecode() Functions for URL Encoding and Decoding

This article explains how PHP's urlencode() and urldecode() functions work, shows their syntax and practical code examples, compares them with rawurlencode()/rawurldecode(), and discusses when to use each function to correctly handle special characters in URLs during web development.

php中文网 Courses
php中文网 Courses
php中文网 Courses
Using PHP urlencode() and urldecode() Functions for URL Encoding and Decoding

In PHP, URL encoding and decoding are very common operations. URL encoding converts special characters in a URL (such as spaces, slashes, question marks, etc.) into their corresponding encoded values, while URL decoding converts those encoded values back to the original characters.

PHP provides a set of functions for these tasks; this article introduces the commonly used urlencode() and urldecode() functions and provides code examples.

1. urlencode() function

The urlencode() function encodes a string for use in a URL. Its syntax is:

string urlencode(string $str)

Example:

$url = "https://www.example.com/index.php?id=123&name=John Doé";
$encoded_url = urlencode($url);
echo $encoded_url;

// Output: https%3A%2F%2Fwww.example.com%2Findex.php%3Fid%3D123%26name%3DJohn+Do%C3%A9

The example shows a URL containing special characters being encoded, with the result displaying the appropriate percent‑encoded values.

2. urldecode() function

The urldecode() function decodes a URL, converting encoded values back to their original characters. Its syntax is:

string urldecode(string $str)

Example:

$encoded_url = "https%3A%2F%2Fwww.example.com%2Findex.php%3Fid%3D123%26name%3DJohn+Do%C3%A9";
$decoded_url = urldecode($encoded_url);
echo $decoded_url;

// Output: https://www.example.com/index.php?id=123&name=John Doé

The example demonstrates that the encoded URL is correctly restored to its original form.

Besides urlencode() and urldecode() , PHP also offers rawurlencode() and rawurldecode() functions, which behave similarly but handle certain characters slightly differently.

Using these URL encoding and decoding functions ensures that special characters in URLs are processed correctly, preventing broken links or malformed query parameters. In real‑world development, you often need to encode or decode user‑provided URLs to maintain safety and accuracy.

In summary, PHP's urlencode() and urldecode() functions provide convenient ways to perform URL encoding and decoding. Depending on the specific requirements, you can choose the appropriate function to achieve the desired result.

Java learning materials download

C language learning materials download

Front‑end learning materials download

C++ learning materials download

PHP learning materials download

Backend DevelopmentPHPURL encodingurlencodeurldecode
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.