Understanding the http_build_query()
Function in PHP
The http_build_query()
function in PHP is used to generate a URL-encoded query string from an associative array or an object. This function is particularly useful when you need to pass parameters via a URL or submit data in a form where URL encoding is required.
When you need to send data in a URL format, especially for making API calls or creating links with query parameters, the http_build_query()
function simplifies this process by converting data into a string that can be appended to URLs. It's also commonly used when working with HTTP GET requests or forms.
Syntax of http_build_query()
http_build_query($data, $prefix = '', $arg_separator = '&', $enc_type = PHP_QUERY_RFC1738)
Parameters:
$data
: The data to be encoded. It can be an associative array, an indexed array, or an object.$prefix
(Optional): A prefix for the query string. If provided, this prefix will be added before the parameter names.$arg_separator
(Optional): A separator between arguments. The default is&
, but you can specify another character like;
.$enc_type
(Optional): This defines the encoding type. It defaults toPHP_QUERY_RFC1738
, which encodes spaces as%20
. Alternatively, you can usePHP_QUERY_RFC3986
for encoding spaces as+
.
Example 1: Basic Usage with Associative Array
<?php
$data = array('name' => 'John Doe', 'age' => 30, 'city' => 'New York');
$queryString = http_build_query($data);
echo $queryString;
?>
Output:
name=John+Doe&age=30&city=New+York
In this example, we created an associative array with name
, age
, and city
as keys. The http_build_query()
function converts this array into a query string that can be appended to a URL.
Example 2: URL Encoding with Custom Separator
<?php
$data = array('name' => 'Alice', 'job' => 'Engineer', 'location' => 'London');
$queryString = http_build_query($data, '', ';');
echo $queryString;
?>
Output:
name=Alice;job=Engineer;location=London
Here, we specified a custom separator ;
instead of the default &
. This allows you to customize how the parameters are joined in the query string.
Example 3: Using Prefix for Nested Arrays
<?php
$data = array('user' => array('name' => 'Bob', 'email' => 'bob@example.com'));
$queryString = http_build_query($data, 'data_');
echo $queryString;
?>
Output:
data_user%5Bname%5D=Bob&data_user%5Bemail%5D=bob%40example.com
In this example, a prefix (data_
) is added to each parameter, and nested arrays are encoded correctly.
Example 4: URL Encoding with Objects
<?php
class User {
public $name = 'Eve';
public $age = 28;
}
$user = new User();
$queryString = http_build_query($user);
echo $queryString;
?>
Output:
name=Eve&age=28
Objects can also be passed to http_build_query()
, and their public properties are encoded in the query string.
At Online Learner, we're on a mission to ignite a passion for learning and empower individuals to reach their full potential. Founded by a team of dedicated educators and industry experts, our platform is designed to provide accessible and engaging educational resources for learners of all ages and backgrounds.
Copyright 2023-2025 © All rights reserved.