MeshWorld India LogoMeshWorld.
PHPTutorial1 min read

How to build query string from an array with http_build_query in PHP

(Updated: Jun 22, 2026)
Listen to ArticleAI Speech
~1 min read narration
100%
How to build query string from an array with http_build_query in PHP

PHP has built in function http_build_query that can be used to prepare a query string from an array.

Instead of explicitly appending elements to string, this function comes handy, which takes cares of generating URL-encoded query string either from an associative or indexed array.

Also it works with an object, comprising of public properties only into final query string.

Supported in PHP >= 5

Example 1

$data = [
  'search' => 'cars',
  'sourceType' => 'image',
];

echo http_build_query($data);

/*
 * Output
 *
 * "search=cars&sourceType=image"
*/

Example 2

$data = [
    'firstName' => 'Shirley',
    'lastName' => 'Setia',
    'profession' => 'Singer',
    'location' => [
        'city' => 'Daman',
        'state' => 'Gujarat',
        'country' => 'India',
    ],
];

echo http_build_query($data);

/*
 * Output
 *
 * "firstName=Shirley&lastName=Setia&profession=Singer&location%5Bcity%5D=Daman&location%5Bstate%5D=Gujarat&location%5Bcountry%5D=India"
*/

Hope you find it useful.

Happy coding

Reader Quality Feedback

Did this technical guide help solve your problem?

Suggest Errata ($0)
Vishnu
Primary Author

Vishnu

Founder & Principal Architect at MeshWorld. Senior engineer and instructor specializing in AI agent systems, scalable web architecture, and modern development workflows.

Explore Author Archive
Compute Fuel & Open Testbed
100% Independent & Verified

Fuel High-Density, Zero-Fluff Engineering Deep-Dives

Every guide on MeshWorld is validated on physical Linux nodes and reproducible testbeds. If this article saved you hours of debugging or unblocked production, consider funding our next cluster run.

Weekly Dispatch

Join MeshWorld Dispatch

Get practical tutorials, system blueprints, and curated AI engineering notes straight to your inbox. No fluff, zero spam.

Zero spam. 1-click unsubscribe anytime.Prefer RSS?
Curated Continuations

Up Next in This Domain.

Browse Full Archive