PHP 8 reference · String Functions
PHP urlencode() Function
Encode a string for a URL query value: spaces become + and special characters become %XX codes.
PHP 8 Since 4.0
Syntax
urlencode(string $string): string
Parameters
| Parameter | Description |
|---|---|
string |
The input string. |
Return value
The encoded string.
Examples
<?php
$q = "tea & cake?";
echo "https://example.com/search?q=" . urlencode($q), "\n";
Output
https://example.com/search?q=tea+%26+cake%3F
<?php
echo urlencode("a b"), " ", rawurlencode("a b"), "\n";
echo urldecode("tea+%26+cake%3F"), "\n";
Output
a+b a%20b tea & cake?
Version history
| Version | Change |
|---|---|
| PHP 4.0 | Added |
In other languages
encodeURIComponent()· JavaScript
Related
rawurlencode()· Encode a string per RFC 3986: spaces become %20. Use it for URL path segments.http_build_query()· Build a URL-encoded query string, such as page=2&sort=name, from an array. Nested arrays become key[]=... pairs.base64_encode()· Encode binary data or text as Base64, using only letters, digits, + and /, so it travels safely as text.- PHP Strings · tutorial