Generate URL-encoded Query String Using PHP | Free Source Code

Generate URL-encoded Query String using PHP

June 1, 2020
PHP
generate url query string

Setting up a Data

First, are going to set up the data that we are going to be using as a sample to build our URL-encoded query string.

Please create a new file, name it as index.php and paste the codes below.

<?php
    $data = array(
        'id' => 1,
        'firstname' => 'neovic',
        'lastname' => 'devierte',
        'address' => 'silay city'
    );
 
    print_r($data);
 
    $url_encoded = http_build_query($data);
 
    echo "
        <br><br>
        <a href='goto.php?".$url_encoded."'>Send to URL</a>
    ";
?>

Creating a Go to Page

Next, we create the goto page where we retrieve our URL-encoded query string.

Please create a new file, name it as goto.php and paste the codes below.

<?php
    echo "
        <h4>Displaying the URL Encoded Data</h4>
        <p>ID: ".$_GET['id']."</p>
        <p>Firstname: ".$_GET['firstname']."</p>
        <p>Lastname: ".$_GET['lastname']."</p>
        <p>Address: ".$_GET['address']."</p>
    ";
?>

Note: http_build query will work on PHP v5 or higher.

That ends this tutorial. Happy Coding!

Download Code
Facebook Comments

Leave a Reply

Your email address will not be published.