Application of extract function in PHP

Source: Internet
Author: User
Tags numeric tostring valid

The extract function is used to decompose a number into variables for direct use, and the following is the interpretation of the extract: The PHP () 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 specifies how the extract () function treats such a conflict when a variable already exists and an element with the same name is in the array. This function returns the number of variables that were successfully set.

The following table is the parameter description:

Grammar

Extract (Array,extract_rules,prefix)

Parameter description

Array required. Specify the input to use.

Extract_rules

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

The processing of key names for illegal, numeric, and conflicting keys is determined according to 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 existing variables. (ignores elements of 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 processing of digital indexes.

Extr_prefix_all-Prefix all variable names PREFIX (third argument).

Extr_prefix_invalid-prefix PREFIX only for illegal or numeric variable names. This tag is a new addition to PHP 4.0.5.

Extr_if_exists-overwrites their values only if there is already a variable of the same name in the current symbol table. Nothing else is to be dealt with. You can use an instance where you have defined a set of valid variables and then extract the values from an array such as $_request to overwrite these variables. This tag is a new addition to PHP 4.2.0.

Extr_prefix_if_exists-the variable name with the prefix appended is established only if there is already a variable with the same name in the current symbol table. This tag is a new addition to PHP 4.2.0.

Extr_refs-Extracts a variable as a reference. This is a powerful indication 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. Please note that prefix is required only if the Extract_type value is Extr_prefix_same,extr_prefix_all,extr_prefix_invalid or extr_prefix_if_exists. If the result appended with a 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 array key names.

This is useful when you get a row of data from a database, so let's look at the following example

Copy code <?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_t Ype,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 ');     $MO vie_leadactor=0;     $movie _director=0; ?> <html>     <head>         <title><?php echo ucfirst ($_get[' acti On ']);? > movie</title>         <style type= "Text/css" ></style>     </head > &nbsP   <body>          <form action= "commit.php?action=<?php echo $_get[' action '];? >&type=movie "method=" POST ">              <table>                 <tr>                     <T D>movie name</td>                     <td><input type= "Text" name= "Movie_name" value= "<?php echo $movie _name;? > "/></td>                 </tr>         &NBS P       <tr>                     <td>movie Type </td>                     <td><select name= "Movie_type" I D= "" >                    php                          $query = ' Select Movietype_i D,movietype_label from Movietype to Movietype_label ';                         $result = mysql_query ($query, $db) or Die (Mysql_error ($db));                         while ($row = MYSQL_FETCH_ASSOC ($result )                         {        &NBSP ;                   if ($row [' movietype_id '] = = $movie _type)     &NBSP ;                       {            & nbsp                   echo ' <option value= '. $row ["movietype_id"]. "Selected= "Selected" > $row ["Movietype_label"]. ' </option> ';                            }       & nbsp                     else             &NBSP ;               {                    & nbsp           echo ' <option value= '. $row ["movietype_id"]. ' " > '. $row ["Movietype_label"]. ' </option> ';                            }       & nbsp                                   & nbsp;?>                     </select></td>                 </tr>               &NBSP <tr>                     <td>movie year</td>   &N Bsp                 <td><select name= "movie_year" id= "" >     &NB Sp               <?php                  &N Bsp       for ($yr = date (' Y '); $yr >1970; $yr-)                         {                            if ($yr = = $movie _year)                             {                                 Echo Option Value= "'. $yr." "Selected=" Selected ">". $yr. ' </option> ';                 &nbsp                                   & nbsp   Else                             {  &NB Sp                             echo ' <option value= '. $yr. ' > ' $yr. ' </option> ';                            }       & nbsp                                   & nbsp;?>                     </select></td>                 </tr>                 <tr>                     <td>lead ACtor</td>                     <td><select name= "Movie_lead Actor "id=" ">                     <?php                          $query = ' Select People_id,people_fullname from people whe Re people_isactor = 1 order by People_fullname ';                         $result = mysql_query ($query, $db) or D IE (mysql_error ($db));                         while ($row = MYSQL_FETCH_ASSOC ($result )                         {        &NBSP ;                   if ($row ["people_id"] = = $movie _leadactor)     &NB Sp                       {                                Echo ' <option value= '. $row ["people_id"]. ' selected= ' selected ' > '. $row ["People_fullname"]. ' </option> ';                            }       & nbsp                     else             &NBSP ;               {                    & nbsp           echo ' <option value= '. $row ["people_id"]. ' " > '. $row ["People_fullname"]. ' </option> ';                            }       & nbsp                                  &NBS P          {                   ?>   &N Bsp                 </select></td>           &NBS P     </tr>                 <tr>         & nbsp           <td>Director</td>                 & nbsp   <td><select name= "Movie_director" id= "" >                     <?php                         $query = ' SELECT * FR Om people where people_isdirector=1 order by People_fullname ';                         $result = mysql_query ($query, $db) or Die (Mysql_error ($db));           &NBSP             while ($row = Mysql_fetch_assoc ($result))           &NBS P             {                      & nbsp     if ($row [' people_id '] = = $movie _director)                   &NBS P                                   & nbsp     echo ' <option value= '. $row [' people_id ']. ' selected= ' selected ' > '. $row ["People_fullname"]. ' </option> ';                            }       & nbsp                     else             &NBSP ;               {                &NBSp               echo ' <option value= '. $row [' people_id ']. ' > '. $row ["People_fullname"]. ' </option> ';                            }       & nbsp                                   & nbsp;?>                     </select></td>                 </tr>                 <tr>                     <TD colspan= "2" style= "Text-align:center" > &nb Sp                       <?php          &N Bsp               if (' edit ' = $_get[' action '])                         {                  & nbsp         echo ' <input type= ' hidden ' value= '. $_get[' id ']. ' name= ' movie_id '/> ';                                   & nbsp            ?>                     &NBS P   <input type= "Submit" name= "Submit" value= "<?php Echo ucfirst ($_get[' action '));? > "/>                     </td>         & nbsp       </tr>             </table>         < /form>     </body> </html> Copy Code

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

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.