Getting Started with PHP and MySQL (6)

Source: Internet
Author: User
Tags getting started with php urlencode
Such loops can be used to handle a long list of things (for example, jokes stored in a database), and here's a simple example: count to 10.

$count = 1;
while ($count <= 10) {
Echo ("$count");
$count + +;
}

I know this sentence may seem scary, but we can look at it in line. The first line defines a variable called $count and assigns it a value of 1. The second line is the start of the while loop, with the condition that $count is less than or equal to (<=) 10. The third and fourth lines are the loop body of the while loop, and we will repeat it when the condition is true. The third line simply outputs a $count value and adds a space after it. The $count value plus one ($count + + is the abbreviation for $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 condition is true. The value of the $count (1) is output, then $count is given a new value of 2. The condition is still true when the condition is checked for the second time, so 2 is output and a new value of 3 is assigned. The process was continued, with outputs of 3, 4, 5, 6, 7, 8, 9 until 10. Finally, the $count is given a value of 11, the condition is false, and the loop ends. The end result is the output of a string "1 2 3 4 5 6 7 8 9 10".

In the condition of this example we use a new operator: <= (less than or equal to). Other operators for numeric comparisons are >= (greater than or equal to),< (less than),> (greater than) and! = (not equal to). The last one can also be used in string comparisons.

Multi-purpose pages

If you want to display the name of the visitor at the top of each page of the website you are creating. Using the example of our previous auto-display welcome message, we've basically succeeded in half. Now we just need to solve these problems for our example:

We need to display every page in the site, not just on one page.

We have no control over which page on our site will be displayed first.

The solution to the first problem is not too difficult. When we get the user name variable on a page, we can pass the variable in any request by adding it to a query string:

"> A link

Please note that we embed PHP code in the middle of the HTML identifier. In fact, this is very common. We are already familiar with the Echo function, but we are not familiar with the UrlEncode function. The function is to convert some special characters in a string, such as spaces, into specific encodings so that they can be displayed in the query string. For example, if the value of the $name variable is "Kevin Yank", where the space is not allowed in the query string, the output of UrlEncode will be Kevin+yank, and the value will be automatically converted back when newpage.php is established in $name.

Ok, now we can pass the user name to the first connection of our site. All we need now is to get its value the first time it appears. In our example above, we have done an HTML page to handle the form that gets the username. The problem is that we can't force users to start on this page every time they visit our site.

  • 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.