PHP http_build_query() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
URL

Definition

In PHP, http_build_query() turns an array into a URL-encoded query string such as name=Ann&age=30.

It encodes special characters for you and can handle nested arrays.

Syntax

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

PHP
$query = http_build_query($data);
CallWhat it does
http_build_query(["a" => 1, "b" => 2])Returns "a=1&b=2".
http_build_query(["q" => "php web"])Returns "q=php+web".

Values equal to null are skipped. true becomes 1 and false becomes 0.

Library declarations
PHP
http_build_query(array|object $data, string $numeric_prefix = "", ?string $arg_separator = null, int $encoding_type = PHP_QUERY_RFC1738): string

Parameters

ParameterDescription
dataRequired. An array or object of values.
numeric_prefixOptional. Text added to numeric keys of a plain array.
arg_separatorOptional. The separator between pairs. Default is the arg_separator.output setting, usually &.
encoding_typeOptional. PHP_QUERY_RFC1738 (default, spaces become +) or PHP_QUERY_RFC3986 (spaces become %20).

Return Value

Return ValueDescription
stringThe URL-encoded query string, without a leading ?.

PHP Examples

Example 1 — Build a Query String

Turn an array of values into a query string.

PHP
<?php
$data = ["name" => "Ann Lee", "age" => 30];

echo http_build_query($data), PHP_EOL;

How It Works

  1. Create an array. Keys become parameter names.
  2. Call http_build_query(). Each pair becomes key=value.
  3. Spaces are encoded. In the default mode a space becomes +.
  4. Pairs are joined with &. There is no leading ?.

Example 2 — Handle Nested Arrays and RFC 3986

Nested arrays use brackets, and the encoding type changes how spaces look.

PHP
<?php
$tags = ["tags" => ["php", "web"]];
echo http_build_query($tags), PHP_EOL;

$name = ["name" => "Ann Lee"];
echo http_build_query($name, "", "&", PHP_QUERY_RFC3986), PHP_EOL;

How It Works

  1. Pass a nested array. Each item gets a key like tags[0].
  2. Brackets are encoded. [ becomes %5B and ] becomes %5D.
  3. Use PHP_QUERY_RFC3986. A space is now written as %20.
  4. Choose by need. Use the default for forms and RFC 3986 for paths and APIs.

Example 3 — Build a Full URL

Add the question mark and join the query to a base URL.

PHP
<?php
$base = "https://example.com/search";

$params = [
    "q" => "php & mysql",
    "page" => 2,
    "sort" => null,
];

echo $base . "?" . http_build_query($params), PHP_EOL;

How It Works

  1. Set a base URL. It has no query yet.
  2. Add parameters. The value null is skipped.
  3. The & in the text is encoded. It becomes %26 so it does not split the pair.
  4. Join with ?. You get a ready-to-use URL.

Frequently Asked Questions

http_build_query() converts an array into a URL-encoded query string that you can add to a URL.
http_build_query(array|object $data, string $numeric_prefix = "", ?string $arg_separator = null, int $encoding_type = PHP_QUERY_RFC1738): string.
No. The result starts with the first key. Add the ? yourself when you build the URL.
true becomes 1, false becomes 0, and null values are left out of the result.
Use parse_url() with PHP_URL_QUERY to get the query text, then parse_str() to turn it into an array.

Next: parse_url()

parse_url() splits a URL into its parts such as host, path and query.

parse_url() tutorial →

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