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

<?php
$content = array('fname'=>'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.

<?php
$content = array('fname', 'xname', 'lanme', 'w3diy', 'cname' => 'infinity', 'php' =>'programming language');

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

The above example will results:

Read Also:  NodeJS RESTful API User Authentication with AngularJS using PHP MYSQL

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

<?php
$content = array('member'=>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

<?php
class masterClass {
    public    $pub      = 'publicmaster';
    protected $prot     = 'protectedmaster';
    private   $priv     = 'privatemaster';
    public    $pub_xname  = Null;
    protected $prot_xname = Null;
    private   $priv_xname = Null;

    public function __construct(){
        $this->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:

Read Also:  PHP Array Length Size Count Tutorial With Example

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

<?php
$vars = array('page' => 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 '<a href="pass.php?link=' . $a . '>Home 1</a>';
echo '<a href="pass.php?link=' . $b . '">Cpntact 2</a>';

Passing data outside

<a href=https://www.domaine-name.com/index.php?id=2489&user=tom>Link to another site</a>

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.