In one of my recent projects, I found a new concept about variables used in PHP. In my program, I need to update multiple records at the same time on a single page, and after quite a long period of painful thinking, I accidentally flashed the concept of variable variables (variable variable), and all the confusion swept away.
Introduced
What is a variable? According to the PHP manual, variables are variables that take the value of a variable and use it as a variable name for another variable. This expression appears 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---and then define a variable variable, meaning 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:
$x = "this";
$ $x = "is cake";
?>
This symbol $$ is the representation of variables in PHP. Now we can refer to these two variables in two different ways $x and $ $x.
echo "$x ${$x}";
?>
echo "$x $this";
?>
Both of the above programs will output this is cake. Notice that in the Echo statement $ $x is written ${$x}, which is to let PHP know that you want to output variable variables instead of a $ character with the $x variable.
Are you still confused? Oh, maybe, you want some more in-depth, more useful examples? In the next section, I'll show you how to use variable variables to edit multiple records on a single page.
Example
Suppose you already have a MySQL database that holds links to some of your favorite sites, with a table submissions in the library, with the following fields:
Submissionid
Postedby
Link
Description
Approved
Now you want to display all the created but not recognized links in the table, the edited page should be able to correct some of the input errors, use the appropriate radio buttons to set the approved for each record, and then commit the updated record to the table at once.
First, when you extract all the records from the database and display them, you must set a unique name for each record, which will allow us to iterate over the values of each record at the time of submission. The code is as follows:
Initializes a variable register
$index = 0;
$index _count = 0;
echo " n ";
echo "
http://www.bkjia.com/PHPjc/631385.html www.bkjia.com true http://www.bkjia.com/PHPjc/631385.html techarticle In one of my recent projects, I found a new concept about variables used in PHP. In my program, I need to update multiple records in one page at the same time, in ...
n "; echo"
Posted by |
Link | "."
Description |
approved |
n ";/********* assumes that we have retrieved records from the database into an array $myrow = Mysql_fetch_array (). The following Do...while loop assigns a value to each $XSTR variable by name and connects $index The value to the end, starting at 0. Thus, for the first time in this loop, the value of the $SUBMISSIONIDSTR is SubmissionID0, the second is SubmissionID1, and so on. /do {$SubmissionIDStr = Submissionid. $index; $PostedByStr = Postedby. $index; $LinkStr = Link $index; $ Descriptionstr = Description. $index; $ApprovedStr = aprroved $index;//This section will display values on the screen, one record per line.