Open In App

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:

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


Article Tags :