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 a considerable amount of painful thought, I accidentally flashed the concept of variable variables (variable variable), and all the confusion was swept away.
<b> Introduction </b>
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:
<?php
$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.
<?php
echo "$x ${$x}";
?>
<?php
echo "$x $this";
?>
Both of the above programs will output this is cake. Note that in the Echo statement $ $x is written as ${$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.
<b> Example </b>
Suppose you already have a MySQL database that holds links to some of the sites you are interested in, 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 input errors, use the appropriate radio buttons to set the Allow (approved) for each record, and then submit the updated records 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:
<?php
Initializes a variable register
$index = 0;
$index _count = 0;
echo "<form method=post action= $PHP _self>n";
echo "<table>n";
echo "<tr><td><b>posted by</b></td><td><b>link</b></td>".
http://www.bkjia.com/PHPjc/445583.html www.bkjia.com true http://www.bkjia.com/PHPjc/445583.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 ...