Variable variables, PHP and you

Source: Internet
Author: User
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&gt; Introduction </b&gt;





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";





?&gt;





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}";





?&gt;





<?php





echo "$x $this";





?&gt;





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&gt; Example </b&gt;





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:





<?php





//Initialization of a variable's register





$index = 0;


$index _count = 0;





echo "<form method=post action= $PHP _self&gt;n";


echo "<table&gt;n";


echo "<tr&gt;<td&gt;<b&gt;posted by</b&gt;</td&gt;<td&gt;<b&gt;link</b&gt;</td&gt;".


"<td&gt;<b&gt;description</b&gt;</td&gt;<td&gt;<b&gt;approved</b&gt;</td&gt;</tr&gt;n";








/*********


assume that we have retrieved records from the database into an array


$myrow = Mysql_fetch_array ().


the Do...while loop below assigns a value to each $XSTR variable according to the name and connects the $index value to the end, starting with 0.


So, the first time this cycle, the $SUBMISSIONIDSTR value 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 a value on the screen, with one record per line.





printf ("<tr&gt;<td&gt;<input type=hidden name=%s value=%s&gt;<input type=text name=%s value=%s&gt;</td&gt;


<td&gt;<input type=text name=%s value=%s&gt;</td&gt;<td&gt;<input type=text name=%s value=%s&gt;</td&gt;


<td&gt;<input type=radio name=%s value=-1&gt;yes<input, Type=radio name=%s value=0 " ,


$SubmissionIDStr, $myrow ["Submissionid"], $PostedByStr, $myrow ["Postedby"], $LinkStr, $myrow ["Link"],


$DescriptionStr, $myrow ["Description"], $ApprovedStr, $APPROVEDSTR);








//per cycle register plus 1





$index + +;


$index _count++;





} while ($myrow = Mysql_fetch_array ($result));





//Create an index register Index_count to track the number of records





echo "<input type=hidden name=counter value= $index _count&gt;n";





echo "<input type=submit&gt;</form&gt;n";





?&gt;




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





for ($index = 0; $index <= $counter; $index + +) {








/*****


This section sets a new variable with the name we created earlier


starts from 0 until $index_count
.

*****/





$varSubmissionID = ' Submissionid '. $index;


$varPostedBy = ' postedby '. $index;


$varLink = ' Link '. $index;


$varDescription = ' Description '. $index;


$varApproved = ' Approved '. $index;


/******


This is the variable part of the variable, assigning each value to the name of each new variable.


For example, when the first loop is assigned to the record SubmissionID0 is the value from the front, we use the variable variable to get it.


*******/





$SubmissionIDvalue = $ $varSubmissionID;


$PostedByvalue = $ $varPostedBy;


$Linkvalue = $ $varLink;


$Descriptionvalue = $ $varDescription;


$Approvedvalue = $ $varApproved;








//Update database





$sql = "UPDATE submissions SET postedby= ' $PostedByvalue ', link= ' $Linkvalue ',".


"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);


}





}





?&gt;





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.











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.