Technical FAQ
PHP Manual
CSS2 Manual
HTML Manual
JS Guide
JS Reference
PhpDock Manual
Nu-Coder Manual
PhpExpress Manual
PHP Joomla
Learn PHP
Last updated: Tue, 19 Sep 2006


(PHP 5)

http_build_query -- Generate URL-encoded query string


string http_build_query ( array formdata [, string numeric_prefix [, string arg_separator]] )

Generates a URL-encoded query string from the associative (or indexed) array provided. formdata may be an array or object containing properties. A formdata array may be a simple one-dimensional structure, or an array of arrays (who in turn may contain other arrays). If numeric indices are used in the base array and a numeric_prefix is provided, it will be prepended to the numeric index for elements in the base array only. This is to allow for legal variable names when the data is decoded by PHP or another CGI application later on.

Note: arg_separator.output is used to separate arguments, unless the arg_separator parameter is specified, which is then used.

Example 1. Simple usage of http_build_query()

$data = array('foo'=>'bar',
              'php'=>'hypertext processor');

echo http_build_query($data); // foo=bar&baz=boom&cow=milk&php=hypertext+processor
echo http_build_query($data, '', '&amp;'); // foo=bar&amp;baz=boom&amp;cow=milk&amp;php=hypertext+processor


Example 2. http_build_query() with numerically index elements.

$data = array('foo', 'bar', 'baz', 'boom', 'cow' => 'milk', 'php' =>'hypertext processor');

echo http_build_query($data);
/* Outputs:

echo http_build_query($data, 'myvar_');
/* Outputs:

Example 3. http_build_query() with complex arrays

$data = array('user'=>array('name'=>'Bob Smith',
              'pastimes'=>array('golf', 'opera', 'poker', 'rap'),

echo http_build_query($data, 'flags_');

this will output : (word wrapped for readability)


Note: Only the numerically indexed element in the base array "CEO" received a prefix. The other numeric indices, found under pastimes, do not require a string prefix to be legal variable names.

Example 4. Using http_build_query() with an object

class myClass {
    var $foo;
    var $baz;

    function myClass() {
        $this->foo = 'bar';
        $this->baz = 'boom';

$data = new myClass();

echo http_build_query($data); // foo=bar&baz=boom


Note: The arg_separator parameter was added in PHP 5.1.2.

See also: parse_str(), parse_url(), urlencode(), and array_walk()

Last updated: Tue, 19 Sep 2006