php

How to extract $_GET / $_POST parameters in PHP

How the contents from the superglobal arrays $_GET and $_POST can be transferred into normal variables?

If the register_globals setting has been deactivated (which should be the case), the GET and POST parameters can only be accessed via the superglobals of the same name ($_GET, $_POST). As soon as register_globals is switched on, however, a variable with the same name is created for each parameter. When calling up http://www.example.com/index.php?name=Alex&age=25, for example, the variables “name” (with value “Alex”) and “age” (with value “25”) would be generated fully automatically. To simulate this behavior, the function extract() can be used on $_GET and / or $_POST. The function reads all key-value pairs and creates variables with the key as name and the value as variable content.
 

 

Example : extract()

The following example applies extract() to some sample GET data.

<?php
    // Fill $_GET with sample data
    $_GET = array('name' => 'Alex', 'age' => '25');
 
    // $name doesn't exist yet, so that should produce an error
    var_dump($name);
 
    // Extract parameters from $_GET
    extract($_GET);
 
    // Output extracted parameters
    // this time the output of $name should not produce an error
    var_dump($name);
    var_dump($age);
?>

Output:

Notice:  Undefined variable: name in [...][...] on line 6
NULL
string(4) "Alex"
string(2) "25"

We can do the same thing to extract data from $_POST array.
PHP MCQ - Multiple Choice Questions and AnswersPHP MCQ – Multiple Choice Questions and Answers – Basics – Part 1This collection of PHP Multiple Choice Questions and Answers (MCQs): Quizzes & Practice Tests with Answer focuses on “PHP basic”.   1. In PHP, variables…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 *