php

How to use $_GET in PHP?

The application of the superglobal $_GET array, especially in connection with forms.

$_GET is an array that contains all parameters from the URL called up by the visitor. For example, if the requested URL is http://www.example.com/index.php?name=alex&id=10001&age=25, then $_GET would contain the following parameters:

  • name = alex
  • id = 10001
  • age = 25

Accordingly, the $_GET array would be identical to array(‘name’ => ‘alex’, ‘id’ => ‘10001’, ‘age’ => ’25’). (All parameter keys and values are stored in the $_GET array as strings).

The $_GET array is a superglobal variable, so it can be called from anywhere in the script at any time. If no parameters were passed, the array is present but empty.
 

 

Example : URL with parameters

Assuming the following script is called via http://www.example.com/test.php?func=echo_message&message=welcome_to_stackhowto:

<?php
    // Function for outputting a message passed by the user
    function echoMessage($msg) {
        // since the message was passed by the user, the content here must not be trusted blindly
        // HTML control characters are therefore encoded
        echo(
            htmlentities($msg, ENT_QUOTES, 'UTF-8')
        );
    }
 
    // Read out "func" parameter
    // Attention: this parameter might not have been passed
    // (URL would be e.g .: http://www.example.com/test.php?message=welcome_to_stackhowto)
    // therefore you have to check here whether it is actually available
    $func = (isset($_GET['func']) ? $_GET['func'] : '');
     
    switch ($func) {
        // show_message as a value for func
        // a message should be displayed
        case 'echo_message':
            $message = (isset($_GET['message']) ? $_GET['message'] : '');
            echoMessage($message);
            break;
 
        // ... here could be inserted further case's,
        // so the visitor can call other functions
 
        // default output if function unknown/not passed
        default:
            echo("No function selected!");
    }
?>

Output:

welcome_to_stackhowto
 

Example : GET-based on form

In the next example, we will create a small form and sends its data via GET.

As soon as the form is submitted, the browser automatically appends the form’s input to the URL as a parameter. So there is no difference for the PHP code between submitting the form and calling it directly via URL with parameters.

GET should only be used with forms if small amounts of data are involved, since all data is appended to the URL. An example of this could be a search function in which only about one to four words are entered as a rule.
<?php
    if (isset($_GET['keywords'])) {
        echo('Searched for ' . htmlentities($_GET['keywords'], ENT_QUOTES, 'UTF-8') . '<br>'."\n");
        echo('Results: None.');
    }
?>
 
<form method="GET" action="?">
    <input type="text" name="keywords" />
    <input type="submit" value="Search" />
</form>

Output after submitting the form (with input “StackHowTo”):

Searched for StackHowTo
PHP MCQ - Multiple Choice Questions and AnswersPHP Questions and Answers – Object-Oriented OOP – Part 1This collection of PHP Multiple Choice Questions and Answers (MCQs): Quizzes & Practice Tests with Answer focuses on “Object-Oriented OOP”.   1. The concept of…Read More
mcqMCQPractice competitive and technical Multiple Choice Questions and Answers (MCQs) with simple and logical explanations to prepare for tests and interviews.Read More

Leave a Reply

Your email address will not be published. Required fields are marked *