Variables, PHP and you

Source: Internet
Author: User
In a recent project, I invented a new concept about applying variables in PHP. In my program, I need to update multiple records at the same time on a page. after a long time of painstaking thinking

In a recent project, I invented a new concept about applying variables in PHP. In my program, I need to update multiple records at the same time on a page. after a long time of painstaking thinking, the concept of a variable (variable) suddenly emerges in my mind, and all the confusions are swept away.

Xianrong

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 straightforward and easily mixed with the word "variable" applied in a sentence. For a simple example, you define a variable --- x is 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 as follows:

<? Php

$ X = 'eas ';
$ X = 'is cake ';

?>

The $ symbol represents a variable in PHP. Now we can use two different methods to reference these two variables $ x and $ x.

<? Php

Echo '$ x $ {$ x }';

?>

<? Php

Echo '$ x $ this ';

?>

The above two programs will output this is cake. Note that $ x in the echo statement is written as $ {$ x}, which makes PHP know 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 useful examples? In the next section, I will show you how to use variable variables to compile multiple records on one page.

Example

Assume that you already have a MySQL database that retains links to sites you are interested in. the database has a table named submissions. The fields are as follows:

SubmissionID
PostedBy
Link
Description
Approved

Now you want to display all created but not recognized links in the table. This compiled page should be able to correct some errors at the time of entry, use the appropriate single-choice button to set whether to accept each record, and then 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:

<? Php

// Initialize the variable counter

$ Index = 0;
$ Index_count = 0;

Echo '<form method = post action = $ PHP_SELF> N ';
Echo '<table> N ';
Echo '<tr> <td> <B> Posted By </B> </td> <B> Link </B> </td> '.
'<Td> <B> Description </B> </td> <B> Approved </B> </td> </tr> N ';


/*********
Assume that records have been retrieved from the database to an array.
$ Myrow = mysql_fetch_array ().
The following do... while loop assigns a value for each $ xstr variable according to its name and connects the value of $ index to the end, starting with 0.
In this way, for the first time in this loop, the value of $ SubmissionIDStr is SubmissionID0, and the value of $ SubmissionID1 is SubmissionID1, and so on.

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.