Thursday, November 20, 2025
HomeLanguagesPHP | http_build_query() Function

PHP | http_build_query() Function

The http_build_query() function is an inbuilt function in PHP which is used to generate URL-encoded query string from the associative (or indexed) array.

Syntax:

string http_build_query( $query_data, $numeric_prefix, $arg_separator, $enc_type = PHP_QUERY_RFC1738 )

Parameters: This function accepts four parameters as mentioned above and described below:

  • $query_data: This parameter holds the array or object containing properties which are given below:
    • It may be a single dimensional array or multi-dimensional array.
    • If $query_data is an object then only public property will be incorporated into the result .
  • $numeric_prefix: This parameter is used if numeric indices are used in the base array, it will be prepended to the numeric index for elements in the base array only.
  • $arg_separator: It is used to separate arguments but may be overridden by specifying this parameter.
  • $enc_type: Its default value is PHP_QUERY_RFC1738.

Return Values: It returns a URL-encoded string.

Below programs illustrate the http_build_query() function in PHP:

Program 1:




<?php
$info = array(
    'sudo' => 'placement',
    'CPP' => 'course',
    'FORK' => 'C',
);
  
echo http_build_query($info) . "#";
echo http_build_query($info, '', '&');
  
?>


Output:

sudo=placement&CPP=course&FORK=C#sudo=placement&CPP=course&FORK=C

Program 2:




<?php
$info = array('neveropen', 'gfg' => 'sudo', 'placement' => 'hypertext processor');
  
echo http_build_query($info) . "$";
echo http_build_query($info, 'myvar_');
?>


Output:

0=neveropen&gfg=sudo&placement=hypertext+processor$myvar_0=neveropen&gfg=sudo&placement=hypertext+processor

Reference: http://docs.php.net/manual/da/function.http-build-query.php

RELATED ARTICLES

Most Popular

Dominic
32404 POSTS0 COMMENTS
Milvus
97 POSTS0 COMMENTS
Nango Kala
6776 POSTS0 COMMENTS
Nicole Veronica
11925 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11994 POSTS0 COMMENTS
Shaida Kate Naidoo
6905 POSTS0 COMMENTS
Ted Musemwa
7160 POSTS0 COMMENTS
Thapelo Manthata
6861 POSTS0 COMMENTS
Umr Jansen
6846 POSTS0 COMMENTS