PHP urlencode() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
URL

Definition

In PHP, urlencode() replaces unsafe characters with % and two hex digits and writes each space as a + sign.

It matches the way HTML forms send data, so it is a good choice for query string values.

Syntax

Call urlencode() with the values you need. Start with this line.

PHP
$safe = urlencode($text);
CallWhat it does
urlencode("a b")Returns "a+b".
urlencode("a&b")Returns "a%26b".

Letters, digits, and the characters - _ . are not encoded. Everything else is.

Library declarations
PHP
urlencode(string $string): string

Parameters

ParameterDescription
stringRequired. The string to encode.

Return Value

Return ValueDescription
stringThe encoded string.

PHP Examples

Example 1 — Encode Text for a Query

Replace spaces and special characters.

PHP
<?php
$text = "Hello World & Co";

echo urlencode($text), PHP_EOL;

How It Works

  1. Start with plain text. It has spaces and an &.
  2. Call urlencode(). Each space becomes +.
  3. The & becomes %26. So it will not split a query pair.
  4. Print the result. It is safe to place in a URL.

Example 2 — See Which Characters Change

The plus, the tilde and the slash are all encoded.

PHP
<?php
echo urlencode("a+b~c/d"), PHP_EOL;

How It Works

  1. Use a mix of characters. It has +, ~ and /.
  2. The + becomes %2B. A real plus must not look like a space.
  3. The ~ becomes %7E. This differs from rawurlencode().
  4. The / becomes %2F. It will not be read as a path separator.

Example 3 — Build a Search Link

Encode the search words and add them to a URL.

PHP
<?php
$term = "php & mysql";

echo "https://example.com/search?q=" . urlencode($term), PHP_EOL;

How It Works

  1. Keep the base URL as it is. Do not encode the ? or =.
  2. Encode only the value. urlencode() handles the spaces and &.
  3. Join the two with `. `. This builds the whole link.
  4. Use http_build_query() for many values. It does this for each pair.

Frequently Asked Questions

urlencode() encodes a string so it can be sent in a URL query. Spaces become + and unsafe characters become %XX.
urlencode(string $string): string. Pass the text and it returns the encoded text.
urlencode() writes a space as + and encodes ~. rawurlencode() writes a space as %20 and keeps ~.
Use it for query string values and form-style data. For URL paths, prefer rawurlencode().
Use urldecode() to turn the encoded text back into the original text.

Back to URL Functions

Browse all PHP URL function tutorials.

URL functions →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful