Definition and Usage
The urlencode()
function is primarily used to encode a string so that it can be safely used as the value of a URL query parameter.
It is especially useful when encoding variable values to be passed to the next page as URL query parameters.
Features
- The
urlencode()
function encodes a given string so that it can be safely used in a URL. - One key feature is that spaces are converted into plus (
+
) signs - This behavior is consistent with the way data is submitted via HTML forms using the
application/x-www-form-urlencoded
MIME type. - The
urlencode()
function is designed to follow the same encoding rules used in HTML form submissions, where spaces are replaced with+
signs - Because of this,
urlencode()
is primarily used to encode string values for use as URL query parameters.
Note:
It has long been a common convention among PHP developers to use urlencode()
when encoding values for URL query parameters.
Basic Example
The following example demonstrates how to use the urlencode()
function to encode a string that will be used as a URL query parameter.
// Value to be passed to the next page
$search = 'hello world!';
// Encode the value for use in a query string
$encodedSearch = urlencode($search);
// Append the encoded value to the URL
$url = 'https://example.com/search.php?q=' . $encodedSearch;
echo $url;
// Output: 'https://example.com/search.php?q=hello+world%21'
In web applications, it's common to accept user input and pass it to another page via the URL. Since query string values can include spaces or special characters, you must encode them properly to ensure a valid URL.
The urlencode()
function is especially useful for encoding variable values that may contain spaces or special characters before passing them to the next page.
Syntax
urlencode(string $string): string
Parameters
$string |
The string to be URL-encoded. |
---|
Return Values
All characters except for alphabetic letters (A–Z, a–z), digits (0–9), hyphens (-
), underscores (_
), and periods (.
) are replaced with a percent sign (%
) followed by two hexadecimal digits. Spaces are encoded as plus signs (+
).
This encoding follows the same rules as data submitted via web forms, specifically the application/x-www-form-urlencoded
MIME type. Browsers interpret spaces as plus signs, which is why urlencode()
is commonly used to encode data collected from HTML forms. It is suitable when adding form data to a URL query string or passing data to another page.
Practical Examples
Here are some practical examples demonstrating the use of the urlencode()
function related to URLs.
Encoding Web Form Strings
The following example shows a simple case of taking user input from a web form and appending it to a URL using the urlencode()
function.
$userinput = 'Data123!@-_ +';
echo "UserInput: $userinput\n";
echo '<a href="mycgi?foo=' . urlencode($userinput) . '">';
/* Output:
'UserInput: Data123!@-_ +'
'<a href="mycgi?foo=Data123%21%40-_+%2B">'
*/
Encoding Query Strings for GET Requests
This example demonstrates how to encode multiple query parameters in a URL using the urlencode()
function, which is useful for safely transmitting user data via GET requests.
$name = 'John Smith';
$age = 20;
$query_string = 'name=' . urlencode($name) . '&age=' . urlencode($age);
$url = "https://example.com/?$query_string";
echo $url;
// Output: 'https://example.com/?name=John+Smith&age=20'
References
See also
- PHP URL Encoding Functions
- PHP rawurlencode() Function – Safely Encoding URI Components
- PHP http_build_query() Function – Generate URL-Encoded Query Strings from Arrays or Objects
- PHP URL Decoding Functions
- PHP urldecode() Function – Decoding URLs Encoded with urlencode()
- PHP rawurldecode() Function – Decoding URLs Encoded with rawurlencode()
- PHP parse_str() Function – Parse URL Query Strings into Variables