Php extract function application _ PHP Tutorial

Source: Internet
Author: User
Extract function application in php. The extract function is used to break down a number into multiple variables for direct use. The W3C explanation is as follows: the PHPextract () function imports the variables from the array to the current symbol table. The extract function in the array is used to break down a number into multiple variables for direct use. The W3C explanation is as follows: the PHP extract () function imports the variable from the array to the current symbol table. For each element in the array, the key name is used for the variable name, and the key value is used for the variable value. The second parameter type is used to specify how the extract () function treats such conflicts when a variable already exists and an element with the same name exists in the array. This function returns the number of successfully set variables.

The following table describes the parameters:

Syntax

Extract (array, extract_rules, prefix)

Parameter description

Array is required. Specifies the input to be used.

Extract_rules

Optional. The extract () function checks whether each key name is a valid variable name and whether it is in conflict with the variable name in the symbol table.

The processing of illegal, numbers, and conflicting key names is determined by this parameter. It can be one of the following values:

Possible values:

EXTR_OVERWRITE-default. If a conflict exists, the existing variables are overwritten.

EXTR_SKIP-if there is a conflict, the existing variables are not overwritten. (Ignore elements with the same name in the array)

EXTR_PREFIX_SAME-if there is a conflict, add the prefix before the variable name. Since PHP 4.0.5, this also includes processing digital indexes.

EXTR_PREFIX_ALL-prefix all variable names (the third parameter ).

EXTR_PREFIX_INVALID-only prefix before invalid or numeric variable names. This mark is newly added to PHP 4.0.5.

EXTR_IF_EXISTS-only overwrite the values of variables with the same name 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. 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.

Prefix

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

This is useful when retrieving a row of data from the database. let's take a look at the following example.

$ Db = mysql_connect ('localhost', 'root', 'ctrip07185419 ') or die ('Can not connect to mysql ');

Mysql_select_db ('moviesite ', $ db) or die (mysql_error ($ db ));

Mysql_query ('set names gbk', $ db );

If (isset ($ _ GET ['action']) & $ _ GET ['action'] = 'edit ')

{

$ Query = 'SELECT movie_name, movie_type, movie_year, movie_leadactor, movie_director FROM movie WHERE movie_id = '. $ _ GET ['id'];

// Echo $ query;

$ Result = mysql_query ($ query, $ db) or die (mysql_error ($ db ));

Extract (mysql_fetch_assoc ($ result ));

}

Else

{

$ Movie_name = '';

$ Movie_type = 0;

$ Movie_year = date ('Y ');

$ Movie_leadactor = 0;

$ Movie_director = 0;

}

?>

<? Php echo ucfirst ($ _ GET ['action']);?> Movie

Note that the red statement can be used directly after extract is used. the variable name is a field name and the variable value is a field value, this is somewhat similar to the method Movie in ado.net to retrieve data from DataReader or DataSet. name = DataSet. table [o]. row [I] ["Name"]. toString () Movie. name = reader ["Name"]. toString ();

The export extract () function imports variables from the array to the current symbol table. For arrays...

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.