Variable variables, PHP, and a project you recently worked on, I found a new concept about using variables in PHP. In my program, I need to update multiple records at the same time on a page. after a long time of hard thinking, the variables of the variable (variablevariable) are occasionally displayed in my mind. PHP and you
In a recent project, I found a new concept about using variables in PHP. In my program, I need to update multiple records at the same time on a page. after a long time of hard thinking, the concept of a variable (variable) suddenly emerges in my mind, and all the confusions are swept away.
Introduction
What is a variable? According to the PHP Manual, a variable refers to the variable name that obtains the value of a variable and uses it as the variable name of another variable. This expression is quite direct and easy to confuse with the word "variable" in a sentence. For a simple example, you define a variable --- x equals to this --- and then define a variable, which means that you use the value of x as the name of the new variable. In this example, the value of this new variable is cake. PHP is used to represent the following:
$ X = "this ";
$ X = "is cake ";
?>
The $ symbol represents the variable in PHP. Now we can reference these two variables $ x and $ x in two different ways.
Echo "$ x $ {$ x }";
?>
Echo "$ x $ this ";
?>
The above two programs will output this is cake. Note: In the echo statement, $ x is written as $ {$ x}, so that PHP knows that the variable you want to output is not a $ character or $ x variable.
Are you still confused? Oh, maybe. do you want some more in-depth and useful examples? In the next section, I will show you how to use variable variables to edit multiple records on one page.
Example
Assume that you already have a MySQL database that stores links to sites of interest. The database contains a table submissions with the following fields:
SubmissionID
PostedBy
Link
Description
Approved
Now you want to display all the created but not recognized links in the table. this editing page should be able to correct some input errors, set whether to allow (Approved) for each record with the appropriate single-choice button, and submit the updated record to the table at one time.
First, when you extract and display all records from the database, you must set a unique name for each record, this will allow us to repeatedly identify the values of each record during submission. The code is as follows:
// Initialize the variable counter
$ Index = 0;
$ Index_count = 0;
Echo "n ";
?>
After the submission, we use the $ index_count variable to traverse all the variables on the page again, and then allocate these variables to other variables, which uses the variable.
// This loop traverses the records displayed on all pages
For ($ index = 0; $ index <= $ counter; $ index ++ ){
/*****
This section uses the name we created earlier to set new variables.
Starting from 0 until $ index_count
*****/
/******
This is the variable part. each value is assigned to the name of each new variable.
For example, in the first loop, SubmissionID0 is assigned to the record from the previous value. We use the variable to get it.
*******/
$ 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 );
}
}
?>
I hope this will help you understand the basic usage of the variable and provide some ideas for using it in your future work. The variable concept initially gave me a headache, but once you know how they work, they become a 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