Codectionary / Developer documentation / PHP

PHP encoded query strings

http_build_query serializes structured parameters into a URL-encoded query string. Explicit RFC3986 encoding uses percent-encoded spaces, avoiding manual concatenation mistakes.

Syntax

http_build_query($params, "", "&", PHP_QUERY_RFC3986)

Examples

PHP encoded query strings

Run as a PHP 8.0+ CLI script, for example php example.php. The multibyte example additionally requires mbstring.

<?php
$query = http_build_query(['q' => 'Lua tables', 'page' => 2], '', '&', PHP_QUERY_RFC3986);
echo '/search?', $query, PHP_EOL;
  1. Output: /search?q=Lua%20tables&page=2. The space is encoded as %20 and the parameter separator remains &.

Best practices

  • Encode parameter values through the serializer. If the final URL is placed into HTML, escape that complete attribute value for HTML too.

Runtime & versions

PHP runtime on a server or CLI. Feature availability depends on the language and runtime version used by your project; consult the official reference below.

PHP Manual

Specifications & further reading