Analysis of PHP function extract () application tips _ PHP Tutorial

Source: Internet
Author: User
Analysis of PHP function extract () application skills. 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: form.html formactionaction.

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:

Form.html

  1. < FormAction = "action. php" method = "post">
  2. < InputType = "text" name = "username">
  3. < InputType = "password" name = "password">
  4. < InputType = "submit">

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

  1. < FormAction = "action. php" method = "post">
  2. < InputType = "text" name = "username">
  3. < InputType = "password" name = "password">
  4. < InputType = "submit">

Is it convenient? The following is a detailed explanation of the PHP function extract () in the PHP manual:

Extract
(PHP 4, PHP 5)

Extract-import the variable from the array to the current symbol table

Description

Int extract (array $ var_array [, int $ extract_type [, string $ prefix])

The PHP function extract () is used to import variables from the array to the current symbol table. The array var_array is used as the parameter and the key name is used as the variable name, and the value is used as the variable value. Each key/value pair will create a variable in the current symbol table and be affected by the extract_type and prefix parameters.

Note: This function returns the number of extracted variables from version 4.0.5.

Note: EXTR_IF_EXISTS and EXTR_PREFIX_IF_EXISTS are introduced in version 4.2.0.

Note: EXTR_REFS is introduced in version 4.3.0.

The PHP function extract () checks each key name to see if it can be used as a valid variable name. it also checks for conflicts with existing variable names in the symbol table. The method for dealing with illegal/numbers and conflicting key names is determined by the extract_type parameter. It can be one of the following values:

EXTR_OVERWRITE
If there is a conflict, overwrite the existing variables.

EXTR_SKIP
If there is a conflict, the existing variables are not overwritten.

EXTR_PREFIX_SAME
If there is a conflict, add the prefix before the variable name.

EXTR_PREFIX_ALL
Prefix all variable names. Since PHP 4.0.5, this also includes processing digital indexes.

EXTR_PREFIX_INVALID
Prefix is added only before invalid/numeric variable names. This mark is newly added to PHP 4.0.5.

EXTR_IF_EXISTS
Override the values of variables with the same name only in the current symbol table. None of them are processed. It can be used for variables that have defined a combination, and then extract values from an array such as $ _ REQUEST to overwrite these variables. This mark is newly added to PHP 4.2.0.

EXTR_PREFIX_IF_EXISTS
Only when a variable with the same name already exists in the current symbol table, the variable name with the prefix is created. other variables are not processed. This mark is newly added to PHP 4.2.0.

EXTR_REFS
Extract variables as references. This effectively demonstrates that the imported variable still references the value of the var_array parameter. This flag can be used independently OR in extract_type OR with any other flag. This mark is newly added to PHP 4.3.0.
If extract_type is not specified, it is assumed to be EXTR_OVERWRITE.

Note that prefix is only required when the value of extract_type is EXTR_PREFIX_SAME, EXTR_PREFIX_ALL, EXTR_PREFIX_INVALID or EXTR_PREFIX_IF_EXISTS. If the result with a prefix is not a valid variable name, it is not imported to the symbol table. An underline is automatically added between the prefix and the array key name.

Extract () returns the number of variables successfully imported into the symbol table.

Warning

Do not use the PHP function extract () for untrusted data, such as user input ($ _ GET ,...). If you do this, for example, to temporarily run the old code dependent on register_globals, make sure that the extract_type value that will not be overwritten is used, such as EXTR_SKIP, and pay attention to php. the order defined by variables_order in ini is extracted.

One possible use of PHP function extract () is to import the content returned by wddx_deserialize () in the Combined Array to the symbol table variable.

Example #1 extract () Example

 
 
  1. <? Php
  2. /* Assume that $ var_array is wddx _
    Array returned by deserialize */
  3. $ Size = "large ";
  4. $ Var_array = array ("color" => "blue ",
  5. "Size" => "medium ",
  6. "Shape" => "sphere ");
  7. Extract ($ var_array, EXTR _
    PREFIX_SAME, "wddx ");
  8. Echo "$ color, $ size, $ shape, $ wddx_sizen ";
  9. ?>

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.

The PHP function extract () must use an associated array. arrays of numeric indexes will not produce results unless EXTR_PREFIX_ALL or EXTR_PREFIX_INVALID is used.


Pipeline form.html form action = action...

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: info-contact@alibabacloud.com 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.