http_build_query – Generate URL-encoded query string Best Examples [PHP Version (5.0 to 7.4)]

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

php Generates a URL-encoded query string from the useful data with associative (or indexed) array provided.

http_build_query
http_build_query

http_build_query syntax Description, Parameters, Return Values and Examples

syntax

string http_build_query ( mixed $query_data [, string $numeric_prefix [, string $arg_separator [, int $enc_type = PHP_QUERY_RFC1738 ]]] )

Here you can Learn to Generate URL-encoded query string Description, Parameters, Return Values and Examples.

Return Values:

It is a Data Returns a URL-encoded string.

php http_build_query Examples

php_function
php_function

Example #1 Simple usage

'xname',
              'lanme'=>'w3diy',
              'cname'=>'infinity',
              'php'=>'programming language');

echo http_build_query($content) . "\n";
echo http_build_query($content, '', '&');

?>

The above example will results:

fname=xname&lanme=w3diy&cname=infinity&php=programming+language
fname=xname&lanme=w3diy&cname=infinity&php=programming+language

Example #2 numerically index elements.

 'infinity', 'php' =>'programming language');

echo http_build_query($content) . "\n";
echo http_build_query($content, 'myvar_');
?>

The above example will results:

0=fname&1=xname&2=lanme&3=w3diy&cname=infinity&php=programming+language
myvar_0=fname&myvar_1=xname&myvar_2=lanme&myvar_3=w3diy&cname=infinity&php=programming+language

Example #3 complex arrays

array('name'=>'Modi saha',
                            'age'=>47,
                            'gender'=>'M',
                            'dob'=>'5/12/1992'),
              'hobby'=>array('play', 'dance', 'cricket', 'dev'),
              'submasterren'=>array('Modiby'=>array('age'=>12,
                                               'gender'=>'M'),
                                'sally'=>array('age'=>8,
                                               'gender'=>'F')),
              'HOD');

echo http_build_query($content, 'temps_');
?>

this will results : (word wrapped for readability)

member%5Bname%5D=Modi+saha&member%5Bage%5D=47&member%5Bgender%5D=M&
member%5Bdob%5D=5%2F12%2F1992&hobby%5B0%5D=play&hobby%5B1%5D=dance&
hobby%5B2%5D=cricket&hobby%5B3%5D=dev&submasterren%5BModiby%5D%5Bage%5D=12&
submasterren%5BModiby%5D%5Bgender%5D=M&submasterren%5Bsally%5D%5Bage%5D=8&
submasterren%5Bsally%5D%5Bgender%5D=F&temps_0=HOD

Note:

Only the support numerically indexed data element in the base array “HOD” received a prefix. as well as The other numeric indices, search under hobby, do not need a data content string prefix to be legal way to applay variable names.

Example #4 Using an object

pub_xname  = new submasterClass();
        $this->prot_xname = new submasterClass();
        $this->priv_xname = new submasterClass();
    }
}

class submasterClass {
    public    $pub  = 'publicsubmaster';
    protected $prot = 'protectedsubmaster';
    private   $priv = 'privatesubmaster';
}

$master = new masterClass();

echo http_build_query($master);
?>

The above example will results:

pub=publicmaster&pub_xname%5Bpub%5D=publicsubmaster

how to use http build query?

Answer:Build GET/POST Query based on associated array.

how to pass parameter in url in php?

here simply Pass simple array Example

how to pass value through url in php
how to pass value through url in php

 45, 'search' => 'authorization');
$qs = http_build_query($vars);
$url = 'http://www.domaine-name.com/search.php?' . $qs;
echo $url;
?>

The above code returns this url query string –

http://www.domaine-name.com/search.php?page=48&search=authorization

how to pass value through url in php?

here Passing variables with data between pages using URL
In your app.php your echo statement must be like this:

echo 'Cpntact 2';

Passing data outside

Link to another site

Difference between GET and POST

Difference between GET and POST
Difference between GET and POST

I hope you get an idea about Generate URL-encoded query string.
I would like to have feedback on my infinityknow.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.

Leave a Comment