HomeMenu
Jesus · Bible · HTML · CSS · JS · PHP · SVG · Applications

http_build_query

Description

The http_build_query of URL for PHP generate URL-encoded query string.

Syntax

http_build_query(
    array|object $data,
    string $numeric_prefix = "",
    ?string $arg_separator = null,
    int $encoding_type = PHP_QUERY_RFC1738
): string

Parameters

data

May be an array or object containing properties.

If data is an array, it may be a simple one-dimensional structure, or an array of arrays (which in turn may contain other arrays).

If data is an object, then only public properties will be incorporated into the result.

numeric_prefix

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.

This is meant to allow for legal variable names when the data is decoded by PHP or another CGI application later on.

arg_separator

The argument separator.

If not set or null, arg_separator.output is used to separate arguments.

encoding_type

By default, PHP_QUERY_RFC1738.

If encoding_type is PHP_QUERY_RFC1738, then the application/x-www-form-urlencoded media type, which implies that spaces are encoded as plus (+) signs.

If encoding_type is PHP_QUERY_RFC3986, then spaces will be percent encoded (%20).

Return

Returns a URL-encoded string.

Examples

1 · data · array · indexed

<?

$data =
[
    "dog",
    "cat",
    "mama llama"
];

$return = http_build_query($data);

echo $return;
0=dog&1=cat&2=mama+llama

2 · data · array · associative

<?

$data =
[
    "a" => "dog",
    "b" => "cat",
    "c" => "mama llama"
];

$return = http_build_query($data);

echo $return;
a=dog&b=cat&c=mama+llama

3 · data · array · multidimensional

<?

$data =
[
    "a" => ["dog", [0, 1]],
    "b" => "cat",
    "c" => ["mama llama", [0, 1, [0, 1]]]
];

$return = http_build_query($data);

echo $return;
a%5B0%5D=dog&a%5B1%5D%5B0%5D=0&a%5B1%5D%5B1%5D=1&b=cat&c%5B0%5D=mama+llama&c%5B1%5D%5B0%5D=0&c%5B1%5D%5B1%5D=1&c%5B1%5D%5B2%5D%5B0%5D=0&c%5B1%5D%5B2%5D%5B1%5D=1

4 · data · object

<?

class myparentclass
{
    public $pub = 'publicparent';
    protected $pro = 'protectedparent';
    private $pri = 'privateparent';

    public $pub2;
    protected $pro2;
    private $pri2;

    public function __construct()
    {
        $this->pub2 = new mychildclass;
        $this->pro2 = new mychildclass;
        $this->pri2 = new mychildclass;
    }
}

class mychildclass
{
    public $pub = 'publicchild';
    protected $pro = 'protectedchild';
    private $pri = 'privatechild';
}

$data = new myparentclass;

$return = http_build_query($data);

echo $return;
pub=publicparent&pub2%5Bpub%5D=publicchild

5 · numeric_prefix

<?

$data =
[
    "dog",
    "cat",
    "mama llama"
];
$numeric_prefix = "myprefix";

$return = http_build_query($data, $numeric_prefix);

echo $return;
myprefix0=dog&myprefix1=cat&myprefix2=mama+llama

6 · arg_separator

<?

$data =
[
    "dog",
    "cat",
    "mama llama"
];
$numeric_prefix = "";
$arg_separator = "#";

$return = http_build_query($data, $numeric_prefix, $arg_separator);

echo $return;
0=dog#1=cat#2=mama+llama

7 · encoding_type · PHP_QUERY_RFC1738

<?

$data =
[
    "dog",
    "cat",
    "mama llama"
];
$numeric_prefix = "";
$arg_separator = null;
$encoding_type = PHP_QUERY_RFC1738;

$return = http_build_query($data, $numeric_prefix, $arg_separator, $encoding_type);

echo $return;
0=dog&1=cat&2=mama+llama

8 · encoding_type · PHP_QUERY_RFC3986

<?

$data =
[
    "dog",
    "cat",
    "mama llama"
];
$numeric_prefix = "";
$arg_separator = null;
$encoding_type = PHP_QUERY_RFC3986;

$return = http_build_query($data, $numeric_prefix, $arg_separator, $encoding_type);

echo $return;
0=dog&1=cat&2=mama%20llama