Content · Web Development · PHP

PHP urlencode() Function: Usage and Examples

Encodes text for application/x-www-form-urlencoded values, with spaces represented by plus signs. Learn how to apply it with examples and common mistakes.

1 views Published Oct 17, 2026 Updated Oct 17, 2026 admin

A common use for urlencode is processing data without rebuilding the operation manually. Encodes text for application/x-www-form-urlencoded values, with spaces represented by plus signs.

What Is urlencode in PHP?


Encodes text for application/x-www-form-urlencoded values, with spaces represented by plus signs.

Syntax:

urlencode(?string $string) : string

Parameters and Return Value


  • $string — A required string input for this operation. Verify the allowed values in the examples and context below.

Declared return type: string. Distinguish false, an empty result, and exceptions where the API permits them.

Example 1: Basic Usage


This example demonstrates the main operation with a small, controlled input.

<?php

echo urlencode("hello world");

The example focuses on the value produced or the state changed by this API. Review the return value and validate input before using this pattern in production.

Example 2: Another Input


This example changes the input or surrounding setup to illustrate another behavior.

<?php

echo urlencode("a+b&c");

The example focuses on the value produced or the state changed by this API. Review the return value and validate input before using this pattern in production.

Practical Example


This application-style example shows a self-contained usage pattern and checks its result.

<?php

$filters = ["search" => "winter sale", "page" => 2];
echo http_build_query($filters);

The example focuses on the value produced or the state changed by this API. Review the return value and validate input before using this pattern in production.

Important Behavior and Edge Cases


  • Prefer http_build_query() for complete query strings instead of concatenating user-controlled values.
  • Use the intended input type and handle empty or malformed values explicitly.
  • Where this API requires an extension, version, or file, document that dependency before using it in a real application.

Comparison With Related Functions


Function or MethodWhen to Use It
urlencodeEncodes text for application/x-www-form-urlencoded values, with spaces represented by plus signs.
rawurlencodePercent-encodes a string as a URL component using RFC 3986 rules, representing spaces as %20.

Common Beginner Mistakes


MistakeCorrect Approach
Using this API without checking the required input or extensionPrefer http_build_query() for complete query strings instead of concatenating user-controlled values.
Encoding an entire URL as a componentEncode only the relevant path segment or query value.
Using decoded values as paths without validationEnforce safe path boundaries after decoding.

Key Takeaways


  • Encodes text for application/x-www-form-urlencoded values, with spaces represented by plus signs.
  • Prefer http_build_query() for complete query strings instead of concatenating user-controlled values.
  • Test both the expected case and at least one failure or empty-input case before deployment.

Encodes text for application/x-www-form-urlencoded values, with spaces represented by plus signs. Use it when the operation fits your input, output and validation requirements.

Share this resourceSend this page to someone who may find it useful.