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, 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.

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


Notice:  Undefined variable: name in [...][...] on line 6
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 *