Query string in php

query string in php


A URL can pass along parameters in the form of query string. PHP can retrieve and get the query string for manipulation and handling in functions and processes.
The easiest and fastest way to get the value of query string in PHP is through $_GET predefined variables. $_GET contains an associative array of variables passed to the current script via the URL parameters, which are passed through urldecode().

Query string parameters as an array

Using $_GET array in PHP we can directly access any key/value pair in the current URL.


Syntax-

$_GET["name"]

For example:

http://www.cyberteak.com/?id=1001&name=cyber


echo $_GET['id']; // Output: 1001
echo $_GET[‘name’];// Output: cyber


Example – Prepare a mini project first page data send on second page using query string.



first.php
<!DOCTYPE html>
<html>
<head>
            <title>Second Page</title>
</head>
<body>
            <?php
                        echo $_GET['name'];
                        echo "<br>";
                        echo $_GET['id'];
                        echo "<br>";
                        echo $_GET['mobile'];
            ?>
</body>
</html>


second.php
<!DOCTYPE html>
<html>
<head>
            <title>Query String</title>
</head>
<body>
            <?php
                        $name = "Ram";
                        $id = 1001;
                        $mobile = '0123456789';
            ?>
            <a href="second.php?name=<?php echo $name; ?>&id=<?php echo $id ?>&mobile=<?php echo $mobile; ?>">Goto Second Page</a>
</body>
</html>


Output – first.php

query string in php first.php output

Output – second.php



Post a Comment

2 Comments