PHP Function Reference

PHP http_build_query() Function



The PHP http_build_query() function generates a URL-encoded query string from the associative (or indexed) array provided.

Syntax

http_build_query(data, numeric_prefix, 
                 arg_separator, encoding_type)

Parameters

data Required. Specify data. It may be an array or object containing properties.
  • If data is an array, it can be a single dimensional array or multi-dimensional array.
  • If data is an object, then only public properties will be incorporated into the result.
numeric_prefix Optional. If numeric indices are used in the base array and this parameter is provided, it will be prepended to the numeric index for elements in the base array only.
arg_separator Optional. arg_separator.output is used to separate arguments but may be overridden by specifying this parameter. arg_separator.output can be set in php.ini.
encoding_type Optional. Specify the encoding type. Default is PHP_QUERY_RFC1738. It can take the following values:
  • PHP_QUERY_RFC1738 : encoding is performed according to RFC 1738 and the application/x-www-form-urlencoded media type, which implies that spaces are encoded as plus (+) signs.
  • PHP_QUERY_RFC3986 : encoding is performed according to RFC 3986, and spaces will be percent encoded (%20).

Return Value

Returns a URL-encoded string.

Example: http_build_query() example

The example below shows the simple usage of http_build_query() function.

<?php
$info = array(
  'lang' => 'php',
  'topic' => 'function-reference',
  'example' => '1',
  'null' => null,
  'php' => 'hypertext processor'
);

echo http_build_query($info)."\n";

//using # as argument separator
echo http_build_query($info, '', '#');
?>

The output of the above code will be:

lang=php&topic=function-reference&example=1&php=hypertext+processor
lang=php#topic=function-reference#example=1#php=hypertext+processor

Example: using numeric index array

The example below shows the usage of numeric_prefix parameter.

<?php
$info = array('php', 'function-reference', 
              '1', null, 'hypertext processor');

echo http_build_query($info)."\n";

//using 'myvar_' as numeric_prefix parameter 
//to prepend it to the numeric index
echo http_build_query($info, 'myvar_');
?>

The output of the above code will be:

0=php&1=function-reference&2=1&4=hypertext+processor
myvar_0=php&myvar_1=function-reference&myvar_2=1&myvar_4=hypertext+processor

Example: using multi-dimensional array

A multi-dimensional array can also be used with this function as shown in the example below:

<?php
$info = array(
  'Mike', 
  'age' => 55,
  'children' => array(
    'John' => array('age'=>12, 'gender'=>'M'),
    'Marry' => array('age'=>8, 'gender'=>'F')
));

echo http_build_query($info, 'var_');
?>

The output of the above code will be (word wrapped for readability):

var_0=Mike&age=55&children%5BJohn%5D%5Bage%5D=12&
children%5BJohn%5D%5Bgender%5D=M&children%5BMarry%5D
%5Bage%5D=8&children%5BMarry%5D%5Bgender%5D=F

Example: using an object

If an object is used, then only public properties is incorporated into the result. Consider the example below:

<?php
class parentClass {
  public    $pub      = 'publicParent';
  protected $prot     = 'protectedParent';
  private   $priv     = 'privateParent';
  public    $pub_bar  = Null;
  protected $prot_bar = Null;
  private   $priv_bar = Null;

  public function __construct(){
    $this->pub_bar  = new childClass();
    $this->prot_bar = new childClass();
    $this->priv_bar = new childClass();
  }
}

class childClass {
  public    $pub  = 'publicChild';
  protected $prot = 'protectedChild';
  private   $priv = 'privateChild';
}

$parent = new parentClass();

echo http_build_query($parent);
?>

The output of the above code will be:

pub=publicParent&pub_bar%5Bpub%5D=publicChild

❮ PHP URLs Reference