Variables in one of my recent projects, I found a new concept about using variables in PHP. In my program, I need to update multiple records on one page at a time, and after a long period of painful thinking, my mind accidentally flashes the concept of variable (variable variable), and all the confusion is swept.
<b> Introduction </b>
What is called a variable? According to the PHP manual, variables are variables that take a variable's value and use it as a variable name for another variable. This expression is quite straightforward and is easily confused with the word "variable" in a sentence. To give a simple example, you define a variable---x equals this---then define a variable, meaning that you take the value of x as the name of the new variable, in this case the value of the new variable is cake. Use PHP to represent the following:
<?php
$x = "this";
$ $x = "is cake";
?>
This symbol $$ is the representation of variable variables in PHP. Now we can refer to these two variables in two different ways $x and $ $x.
<?php
echo "$x ${$x}";
?>
<?php
echo "$x $this";
?>
the above two procedures will output this is cake. Note that in the Echo statement $ $x is written as ${$x}, which is to let PHP know what you want to output is a variable rather than a $ character and $x variable.
Are you still confused? Oh, maybe, you want some more in-depth and useful examples? In the next section, I'll show you how to edit multiple records in one page with variable variables.
<b> Example </b>
Suppose you already have a MySQL database that holds links to some sites of interest, a table submissions in the library, and the fields are as follows:
Submissionid
Postedby
Link
Description
Approved
now you want to show all the created but not recognized links in the table, this edited page should be able to correct some of the input errors and use the appropriate radio button to set the permission for each record (Approved), and then submit the updated records to the table one at a time.
First, when you extract all the records from the database and display them, you have to set a unique name for each record, which allows us to iterate over the values of each record at the time of submission. The code is as follows:
After
commits, we use the $index_count variable to iterate through all the variables on the page, and then assign these variables to other variables, which uses the variable.
<?php
//This loop iterates through the records displayed on all pages
"description= ' $Descriptionvalue ' WHERE submissionid= $SubmissionIDvalue '";
$result = mysql_query ($sql);
//If this record is set to Approved, update the corresponding field Approved.
if ($Approvedvalue = = '-1 ') {
$sql = "UPDATE submissions SET approved= '-1 ' WHERE submissionid= $SubmissionIDvalue";
$result = mysql_query ($sql);
}
}
?>
I hope this will help you understand the basic usage of variable variables and provide some ideas for using them in your future work. Variable variable this concept, at first also gives me a headache, but once you know the basic principles of their work, they become a piece of delicious pizza. If you have any questions, please let me know.
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