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

Create a free account to save this page, take notes and track your progress. Sign in

Syntax

urlencode(string $string): string

Parameters

ParameterDescription
string The input string.

Return value

The encoded string.

Examples

Encode a query valueTry it yourself
<?php
$q = "tea & cake?";
echo "https://example.com/search?q=" . urlencode($q), "\n";

Output

https://example.com/search?q=tea+%26+cake%3F
urlencode() vs rawurlencode()Try it yourself
<?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

VersionChange
PHP 4.0Added

In other languages

Was this page helpful?