Open In App

PHP | http_build_query() Function

Improve
Improve
Like Article
Like
Save
Share
Report

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('geeks', 'gfg' => 'sudo', 'placement' => 'hypertext processor');
  
echo http_build_query($info) . "$";
echo http_build_query($info, 'myvar_');
?>


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



Last Updated : 09 Jul, 2019
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads