Various usage of extract functions in PHP-PHP source code

Source: Internet
Author: User
This article describes in detail the usage of the extract function in PHP, from changing data to variables, post, get data processing, and so on. This article describes in detail the usage of the extract function in PHP, from changing data to variables, post, get data processing, and so on.

Script ec (2); script

Directly run the Code:

The Code is as follows:
$ My_array = array ("a" => "Cat", "B" => "Dog", "c" => "Horse ");
Extract ($ my_array );
Echo "$ a = $ a; $ B = $ B; $ c = $ c ";

Guess what will happen?


The Code is as follows:
$ A = Cat; $ B = Dog; $ c = Horse

No variable $ a $ B $ c is defined, and no value is assigned. Why can we get the value?

For example, you can easily extract $ _ POST or $ _ GET elements. You cannot assign one-to-one values to the content submitted in the form. You can directly use the following code:


The Code is as follows:

In action. php, you only need to use the extract () function to unbind the $ _ POST global data:
Action. php

The Code is as follows:

Extract ($ _ POST );
// Equivalent to $ username = $ _ POST ['username'];
// $ Password = $ _ POST ['Password'];

Array Operations


The Code is as follows:

/* Assume that $ var_array is the array returned by wddx_deserialize */

$ Size = "large ";
$ Var_array = array ("color" => "blue ",
"Size" => "medium ",
"Shape" => "sphere ");
Extract ($ var_array, EXTR_PREFIX_SAME, "wddx ");

Echo "$ color, $ size, $ shape, $ wddx_sizen ";


The above example will output:

Blue, large, sphere, medium
$ Size is not overwritten. Because EXTR_PREFIX_SAME is specified, $ wddx_size is created. If EXTR_SKIP is specified, $ wddx_size is not created. EXTR_OVERWRITE sets the value of $ size to "medium", and EXTR_PREFIX_ALL creates a new variable $ wddx_color, $ wddx_size, and $ wddx_shape.

Array 2

. Applicability: Non-numeric index array with key-value pairs;
2. The variable name created by the function is the keyword in the array, and the variable value is the corresponding value in the array;
3. Two optional parameters: extract_type and prefix;
Extract_type specifies the method used to handle variable name conflicts. The default value is EXTR_OVERWRITE, indicating to overwrite existing variables,
When the value of extract_type is EXTR_PREFIX_ALL, the prefix is added before all created variables. The prefix is provided by the prefix parameter;
4. the keyword of the element in the array must be a valid variable name, otherwise it will be skipped.



The Code is as follows:

Test Array Extract

$ Array = array ('key1' => 'value1', 'key2' => 'value2', 'key3' => 'value3 ');
Extract ($ array );
Echo 'default:
Echo "$ key1 $ key2 $ key3

// Skip an element in case of a conflict
$ Key1 = "abc ";
Extract ($ array, EXTR_SKIP );
Echo 'skip:
Echo "$ key1 $ key2 $ key3

// Add the value provided by the prefix parameter before all variable names
Extract ($ array, EXTR_PREFIX_ALL, 'My ');
Echo 'prefix _ All:
Echo "$ my_key1 $ my_key2 $ my_key3

Is it convenient?
For more information about this function, see

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.