Content · Web Development · PHP

PHP http_build_query() Function: Usage and Examples

Encodes an array or object as an RFC-style query string with percent-encoded keys and values.

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

In PHP, http_build_query() helps with runtime and application data. Encodes an array or object as an RFC-style query string with percent-encoded keys and values.

What Is http_build_query() in PHP?


The function or method has a specific purpose: Encodes an array or object as an RFC-style query string with percent-encoded keys and values. The following examples show the main call, one additional situation, and an application-style use.

Syntax:

<?php
http_build_query(object|array $data, string $numeric_prefix = ..., ?string $arg_separator = ..., int $encoding_type = ...) : string

Parameters and Return Value


  • $data (object|array) — input bytes or source data; required.
  • $numeric_prefix (string) — argument used by this API to perform its operation; optional.
  • $arg_separator (?string) — argument used by this API to perform its operation; optional.
  • $encoding_type (int) — argument used by this API to perform its operation; optional.

Example 1: Basic Usage


Encodes an array or object as an RFC-style query string with percent-encoded keys and values.

<?php
$parameters = ["page" => 2, "search" => "blue sky"];
echo http_build_query($parameters, "", "&", PHP_QUERY_RFC3986);

This example demonstrates the central operation. The final output depends on the supplied input and, where relevant, the runtime or connected service.

Example 2: Handling a Different Case


<?php
$data = ["filters" => ["active" => true, "roles" => ["editor", "author"]]];
echo http_build_query($data, "", "&", PHP_QUERY_RFC3986);

The second example focuses on a different input or failure condition, so the behavior can be distinguished from the basic case.

Practical Example


<?php
$queryParameters = ["sort" => "newest", "category" => "PHP", "page" => 3];
$queryString = http_build_query($queryParameters, "", "&", PHP_QUERY_RFC3986);
echo "/articles?" . $queryString;

The practical example demonstrates how the same subject fits into a larger application. Use your own file paths, endpoints and validated inputs rather than copying sample settings directly.

Important Behavior and Edge Cases


  • Encodes an array or object as an RFC-style query string with percent-encoded keys and values.
  • Check the return value and exceptions for failure before using the result. If an extension or external system is required, the example only works when that dependency is available. Network outcomes are not deterministic and responses should not be assumed to be successful.
  • A response status, media type and headers are part of the HTTP contract; do not expose sensitive details in error bodies.

Comparison With Related Functions


ApproachDifference
http_build_queryEncodes an array or object as an RFC-style query string with percent-encoded keys and values.
parse_strParses query-string data instead of creating it.

Common Beginner Mistakes


MistakeCorrect Approach
Ignoring errors from http_build_queryCheck the actual result and handle the expected error path.
Using the demonstration unchanged in productionAdapt file paths, configuration, authentication and permission rules to the application.
Treating input or returned data as trustedApply validation at input boundaries and context-aware escaping at output boundaries.

Key Takeaways


  • Encodes an array or object as an RFC-style query string with percent-encoded keys and values.
  • Check the return value and exceptions for failure before using the result. If an extension or external system is required, the example only works when that dependency is available. Network outcomes are not deterministic and responses should not be assumed to be successful.
  • A response status, media type and headers are part of the HTTP contract; do not expose sensitive details in error bodies.

Encodes an array or object as an RFC-style query string with percent-encoded keys and values. Use this approach when its capabilities and constraints match the application. Handle invalid data, failure conditions and resource cleanup explicitly.

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