PHP and MySQL (6) can be used to process a long list of things (for example, jokes stored in the database). Below is a simple example: count to 10.
$ Count = 1;
While ($ count <= 10 ){
Echo ("$ count ");
$ Count ++;
} |
I know this statement may seem terrible, but we can look at it in a row. The first line defines a variable named $ count and assigns it to 1. The second line is the beginning of the while loop, and the piece is $ count less than or equal to (<=) 10. The third and fourth rows are the body of the while loop. when the condition is true, we will execute it again. The third line outputs the value of $ count and adds a space after it. In the fourth row, the value of $ count plus one ($ count ++ is the abbreviation of $ count = $ count + 1-the meaning of the two is exactly the same ).
Now we can see how this program is executed. When the condition is checked for the first time, the value of $ count is 1, so the items are true. The $ count value (1) is output, and $ count is assigned a new value of 2. When the second check condition is true, 2 is output and a new value of 3 is assigned. This process continues and outputs 3, 4, 5, 6, 7, 8, 9 until 10. Finally, $ count is assigned a value of 11. the condition is false and the loop ends. The final result is the output of such a string "1 2 3 4 5 6 7 8 9 10 ".
In this example, we use a new operator <= (less than or equal ). Other operators used for numerical comparison include >=( greater than or equal to), <<>>> (greater than), and! = (Not equal ). The last one can also be used in string comparison.
Multi-Purpose page
If you want to display the visitor's name at the top of each page of your website. Using the previous example of automatically displaying welcome information, we have basically achieved half of the success. Now we only need to solve these problems for our example:
We need to display it on every page of the site, instead of just one page.
We cannot control that the page on our site will be displayed first.
The solution to the first problem is not too difficult. After obtaining the username variable on a page, we can pass this variable in any request by adding it to a query string:
Note that PHP code is embedded in the middle of the HTML identifier. In fact, this is very common. We are familiar with the echo function, but we are not familiar with the urlencode function. This function is used to convert some special characters (such as spaces) in a string into specific encoding so that they can be displayed in the query string. For example, if the value of the $ name variable is "Kevin Yank" and the space in the variable cannot exist in the query string, the output of urlencode will be Kevin + Yank, in newpage. when $ name is created in php, the value is automatically converted back.
OK. Now you can pass the user name to the first connection of our site. What we need now is to obtain its value when it first appears. In the above example, we have made an HTML page to process the form for obtaining the user name. The problem is that we cannot force users to start from this page every time they access our site.