Example of the PHP extract function turning a field into a variable

Source: Internet
Author: User

The function of the PHP extract function is to import 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. One trick with the extract function is to make the field variable and easy to invoke.


$info = $this->db->get_one (array (' ID ' => $specialid, ' disabled ' =>0));

Imports the field corresponding to the current topic ID as a variable into the current symbol table

Extract ($info);

This example $info the result from the database, in the form of an array as a variable, the key name is the field name, the value is the field value, use the Extract function as the variable name, the field value as the variable value.

such as Array (' name ' => ' Zhangsan ', ' sex ' => ' man ', ' id ' =>3232);

Then extract ($array), you can directly use $name, $sex, $id.

Look again

<?php
$size = "Old Size"; Note the value of the last size variable.
$a = Array (
"Color" => "red",
"Size" => "XXL",
"Price" => "53");
Extract ($a);
echo "color = $color <br/>";
echo "size = $size <br/>";
echo "Price = $price <br/>";
?>


The results are:

color = Red
Size = XXL
Price = 53

The above example finds that the value of $size is XXL instead of the previous "old size", which means that the original variable is overwritten by default when the key in the array conflicts with an existing variable.


Example

Form.html

<form action= "action.php" method= "POST" >
<input type= "text" name= "username" >
<input type= "password" name= "password" >
<input type= "Submit" >

Use the Extract () function in action.php to unlock the $_post global data:
action.php

<?php
Extract ($_post);
Equivalent to $username = $_post[' username '];
$password = $_post[' password '];
?>

Is it convenient?

Analysis of the function of extract

Addslashes--referencing strings using backslashes

Extract (Addslashes ($_post)); --Handling post forms

Take the variable name from the client <form method= "POST" ...> form.

Extract (Addslashes ($_get)); --Handling Get forms

The


takes the client <form method= "get" ...> the name of the variable in the form.   

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.