Thursday, January 22, 2026
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
32475 POSTS0 COMMENTS
Milvus
119 POSTS0 COMMENTS
Nango Kala
6847 POSTS0 COMMENTS
Nicole Veronica
11977 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12064 POSTS0 COMMENTS
Shaida Kate Naidoo
6986 POSTS0 COMMENTS
Ted Musemwa
7220 POSTS0 COMMENTS
Thapelo Manthata
6933 POSTS0 COMMENTS
Umr Jansen
6912 POSTS0 COMMENTS