Parameters
$str: The input string that needs to be encoded for use in a URL.
Return Value
The function returns a string where all non-alphanumeric characters (except -, _, and .) are replaced with a percent (%) sign followed by two hexadecimal digits. Spaces are replaced with +.
What Exactly Does the `urlencode()` Function Do?
The `urlencode()` function in PHP is used to encode a string so that it can be safely included in a URL. URLs have specific rules about which characters are allowed. For example, spaces, ampersands (`&`), question marks (`?`), & other special characters are not allowed in URLs as they are. If you try to include them directly, the URL might break or behave unexpectedly.
The `urlencode()` function converts these special characters into a format that is safe for URLs. It replaces spaces with `+` & other special characters with a `%` followed by two hexadecimal digits. This process is called URL encoding.
For example, if you have a string like `Hello World!`, the `urlencode()` function will convert it to `Hello+World%21`. Here, the space is replaced with `+`, & the exclamation mark (`!`) is replaced with `%21`.
Let’s look at a complete example to understand how this works:
<?php
// Original string with spaces & special characters
$string = "Hello World! How are you?";
// Using urlencode() to encode the string
$encodedString = urlencode($string);
// Output the encoded string
echo $encodedString;
?>

You can also try this code with Online PHP Compiler
Run Code
Output:
Hello+World%21+How+are+you%3F
In this Code:
1. We start with a string `"Hello World! How are you?"` that contains spaces & special characters like `!` & `?`.
2. The `urlencode()` function is applied to this string.
3. The function replaces spaces with `+` & special characters with their encoded equivalents (`!` becomes `%21` & `?` becomes `%3F`).
4. The encoded string is then displayed using `echo`.
This encoded string can now be safely used in a URL without causing any issues.
Examples
Example 1: Encoding a Simple String
<?php
$input = "Hello World!";
$encoded = urlencode($input);
echo $encoded;
?>

You can also try this code with Online PHP Compiler
Run Code
Output:
Hello+World%21

You can also try this code with Online PHP Compiler
Run Code
Explanation: The space in "Hello World!" is replaced by +, and the exclamation mark (!) is replaced by %21.
Example 2: Encoding a URL with Query Parameters
<?php
$base_url = "https://www.example.com/search";
$query = "php urlencode function";
$encoded_query = urlencode($query);
$url = $base_url . "?q=" . $encoded_query;
echo $url;
?>

You can also try this code with Online PHP Compiler
Run Code
Output:
https://www.example.com/search?q=php+urlencode+function

You can also try this code with Online PHP Compiler
Run Code
Explanation: The spaces in the query string are replaced with + to ensure the URL is properly formatted.
Example 3: Encoding Special Characters
<?php
$input = "name@domain.com";
$encoded = urlencode($input);
echo $encoded;
?>

You can also try this code with Online PHP Compiler
Run Code
Output:
name%40domain.com

You can also try this code with Online PHP Compiler
Run Code
Explanation: The @ symbol is replaced with %40 to ensure safe transmission in a URL.
Example 4: Using urlencode() with Form Data
<?php
$data = [
"name" => "John Doe",
"email" => "john.doe@example.com"
];
$query_string = http_build_query($data);
echo $query_string;
?>

You can also try this code with Online PHP Compiler
Run Code
Output:
name=John+Doe&email=john.doe%40example.com
Explanation: The http_build_query() function applies urlencode() to all values, making it useful for generating query strings dynamically.
Some Important Points Related to the `urlencode()` Function
The `urlencode()` function is simple to use, but there are some key points you should keep in mind to use it effectively. Let’s go through them one by one:
1. When to Use `urlencode()`
You should use `urlencode()` whenever you need to include user input or dynamic data in a URL. For example, if you are creating a search functionality where the user’s query is passed as a URL parameter, you must encode the query to ensure it works correctly.
Example:
<?php
// User's search query
$searchQuery = "PHP urlencode tutorial";
// Encoding the search query
$encodedQuery = urlencode($searchQuery);
// Creating the search URL
$searchURL = "https://example.com/search?q=" . $encodedQuery;
// Output the final URL
echo $searchURL;
?>

You can also try this code with Online PHP Compiler
Run Code
Output:
https://example.com/search?q=PHP+urlencode+tutorial
Here, the space in the search query is replaced with `+`, making the URL valid.
2. Difference Between `urlencode()` and `rawurlencode()`
PHP provides another function called `rawurlencode()`. While both functions encode strings for URLs, they handle spaces differently:
- `urlencode()` replaces spaces with `+`.
- `rawurlencode()` replaces spaces with `%20`.
Example:
<?php
$string = "Hello World!";
echo "urlencode(): " . urlencode($string) . "\n";
echo "rawurlencode(): " . rawurlencode($string);
?>

You can also try this code with Online PHP Compiler
Run Code
Output:
urlencode(): Hello+World%21
rawurlencode(): Hello%20World%21
Use `rawurlencode()` if you need strict compliance with URL standards, as `%20` is the standard encoding for spaces.
3. Decoding URLs with `urldecode()`
If you receive an encoded URL & need to decode it back to its original form, you can use the `urldecode()` function.
Example:
<?php
$encodedString = "Hello+World%21";
// Decoding the string
$decodedString = urldecode($encodedString);
// Output the decoded string
echo $decodedString;
?>

You can also try this code with Online PHP Compiler
Run Code
Output:
Hello World!
This function is useful when processing data received from URLs.
4. Encoding Query Parameters
When building URLs with multiple query parameters, you need to encode each parameter separately. For example:
<?php
$name = "John Doe";
$age = 25;
// Encoding each parameter
$encodedName = urlencode($name);
$encodedAge = urlencode($age);
// Building the URL
$profileURL = "https://example.com/profile?name=" . $encodedName . "&age=" . $encodedAge;
// Output the final URL
echo $profileURL;
?>

You can also try this code with Online PHP Compiler
Run Code
Output:
https://example.com/profile?name=John+Doe&age=25
This ensures that each parameter is correctly encoded & the URL works as expected.
Frequently Asked Questions
What is the difference between urlencode() and rawurlencode()?
Both functions encode URLs, but urlencode() replaces spaces with +, whereas rawurlencode() replaces spaces with %20.
Why should I use urlencode()?
urlencode() ensures that special characters in a URL query string are properly encoded, preventing errors in data transmission.
Can I decode an encoded URL?
Yes, you can use urldecode() to convert an encoded string back to its original form.
Conclusion
In this article, we covered the PHP urlencode() function, which is essential for encoding URLs. The function converts special characters, such as spaces and symbols, into a format that can be safely used in URLs. This ensures that URLs are properly structured and can be transmitted without errors over the internet. Using the urlencode() function is crucial when working with query strings and URL parameters, making it a key tool for PHP developers to ensure data integrity and security in web applications.