The Extract function in PHP

Source: Internet
Author: User

The extract function is used to decompose a number into multiple variables directly, and the following is the explanation of the user: the PHP extract () function imports variables from the array into 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 conflicts when a variable already exists and the element with the same name in the array. This function returns the number of variables that were set successfully.

The following table shows the parameter description:

Grammar
Extract (Array,extract_rules,prefix)
Parameters Description
Array Necessary. Specifies the input to use.
extract_rules

Optional. The extract () function checks to see if each key name is a valid variable name, and also checks whether the variable names in the symbol table conflict.

The handling of illegal, numeric, and conflicting key names is determined by this parameter. Can be one of the following values:

Possible values:

  • extr_overwrite-default. If there is a conflict, overwrite the existing variable.
  • Extr_skip-If there is a conflict, do not overwrite the existing variable. (ignores elements with the same name in the array)
  • extr_prefix_same-If there is a conflict, precede the variable name with the prefix PREFIX. Since PHP 4.0.5, this also includes the processing of the digital index.
  • extr_prefix_all-Prefix all variable names with PREFIX (the third argument).
  • extr_prefix_invalid-prefix PREFIX is only preceded by an illegal or numeric variable name. This tag is a new addition to PHP 4.0.5.
  • extr_if_exists-Overrides the value of a variable with the same name in the current symbol table only if it already exists. None of the others will deal with it. It can be used where a set of valid variables has been defined and then extracted from an array such as $_request to override those variables. This tag is a new addition to PHP 4.2.0.
  • extr_prefix_if_exists-A variable name with the prefix appended to it is created only if a variable with the same name already exists in the current symbol table, and none of the others are processed. This tag is a new addition to PHP 4.2.0.
  • extr_refs-Extracts the variable as a reference. This strongly demonstrates that the imported variable still references the value of the Var_array parameter. This flag can be used alone or in conjunction with any other flag in Extract_type. This tag is a new addition to PHP 4.3.0.
Prefix

Optional. Note that prefix is required only if the value of Extract_type is Extr_prefix_same,extr_prefix_all,extr_prefix_invalid or extr_prefix_if_exists. If the result appended with the prefix is not a valid variable name, it will not be imported into the symbol table.

An underscore is automatically added between the prefix and the array key name.

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

<?PHP$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;}?>Echo Ucfirst($_get[' Action ']);? > movie</title> <style type= "text/css" ></style> $_get[' Action '];? >&type=movie "method=" POST "> <table> <tr> <td>mo Vie name</td> <td><input type= "text" name= "Movie_name" value= "<?php Echo$movie _name;? > "/></td> </tr> <tr> <td>movie type</td&                    Gt <td><select name= "Movie_type" id= "> <?PHP$query= ' Select Movietype_id,movietype_label from Movietype order by Movietype_label '; $result=mysql_query($query,$db) or die(Mysql_error($db));  while($row=Mysql_fetch_assoc($result))                        {                            if($row[' movietype_id '] = =$movie _type)                            {                                Echo' <option value= '.$row["movietype_id"]. ' " Selected= "Selected" > ".$row["Movietype_label"]. ' </option> '; }                            Else                            {                                Echo' <option value= '.$row["movietype_id"]. ' " > '.$row["Movietype_label"]. ' </option> '; }                        }                    ?> </select></td> </tr> <tr> <td>movie year</td> <td><select name= "movie_year" id= "" > & lt;?PHP for($yr=Date(' Y ');$yr>1970;$yr--)                        {                            if($yr==$movie _year)                            {                                Echo' <option value= '.$yr. ' "selected=" selected ">".$yr.‘ </option> '; }                            Else                            {                                Echo' <option value= '.$yr.‘" > '.$yr.‘ </option> '; }                        }                    ?> </select></td> </tr> <tr>                    <td>lead actor</td> <td><select name= "Movie_leadactor" id= "" > <?PHP$query= ' Select People_id,people_fullname from people where People_isactor = 1 order by People_fullname '; $result=mysql_query($query,$db) or die(Mysql_error($db));  while($row=Mysql_fetch_assoc($result))                        {                            if($row["people_id"] = =$movie _leadactor)                            {                                Echo' <option value= '.$row["people_id"]. ' " Selected= "Selected" > ".$row["People_fullname"]. ' </option> '; }                            Else                            {                                Echo' <option value= '.$row["people_id"]. ' " > '.$row["People_fullname"]. ' </option> '; }                                                    }                    ?> </select></td> </tr> <tr>                    <td>Director</td> <td><select name= "Movie_director" id= "" > <?PHP$query= ' SELECT * from people where people_isdirector=1 order by People_fullname '; $result=mysql_query($query,$db) or die(Mysql_error($db));  while($row=Mysql_fetch_assoc($result))                        {                            if($row[' people_id '] = =$movie _director)                            {                                Echo' <option value= '.$row[' people_id ']. ' " Selected= "Selected" > ".$row["People_fullname"]. ' </option> '; }                            Else                            {                                Echo' <option value= '.$row[' people_id ']. ' " > '.$row["People_fullname"]. ' </option> '; }                        }                    ?> </select></td> </tr> <tr> &LT;TD colspan= "2" style= "Text-align:center" > <?PHPif(' edit ' = =$_get[' Action '])                        {                            Echo' <input type= ' hidden ' value= '.$_get["id"]. ' " Name= "movie_id"/> '; }                        ?> <input type= "Submit" name= "Submit" value= "<?php Echo Ucfirst ($_get[' Action ']);? > "/> </td> </tr> </table> </form> &lt ;/body>

Note that the red statement, after using extract can be used directly after the variable, and the name of the variable is the field name, the value of the variable is the value of the field, which is somewhat similar to the method of obtaining data from the DataReader or dataset in ADO Movie.name = Dataset.table[o]. row[i]["Name"]. ToString () movie.name=reader["Name"]. ToString ();

Compared to the PHP in the extract way is not a simple comparison of direct??

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.