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:

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output:

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

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output:

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

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




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.