Getting Started with PHP and MySQL (5)

Source: Internet
Author: User
Control statements

In the example of PHP code we saw earlier, either there was only one single output text statement to the Web page, or a series of statements executed sequentially. If you have written programs in other languages, you should know that there are few such simple programs in practice.
Like any other programming language, PHP provides a convenient way to handle the "control flow" in the script. That is, PHP contains some special statements, which you are not bound by the order in which they are executed sequentially. This statement is called a "control statement." Don't understand? Don't worry! There are some examples that will be very helpful to you.

The most basic and most commonly used control statement is the IF-ELSE statement. It's structured like this:

if ( ) {
If it is true, the statement will be executed.
} else {
(optional) If false, the statement will be executed.
}

This control allows us to tell PHP to decide which set of statements to execute accordingly, depending on the authenticity of some conditions. If you don't think I'm doing a little bit of emptiness, we can modify the welcome.php file we just created to:

if ($name = = "Kevin") {
Echo ("Welcome, OH Glorious leader!");
} else {
Echo ("Welcome, $name!");
}

Now, if the value of the name variable passed to the page is Kevin, a special message is displayed. Otherwise, a generic message that contains the name of the input is displayed.

It should be noted that in this structure, the "Else clause" (which indicates what is being executed when the condition is false in the IF-ELSE structure) is optional. For the above example, if our aim is to display special information when the appropriate name is entered, otherwise nothing will be displayed. The code should look like this:

if ($name = = "Kevin") {
Echo ("Welcome, OH Glorious leader!");
}

The = = used in the above conditions is an operator that compares two values for equality. In particular remember that you need to enter two equals sign here. If you enter an equal sign, you are actually using the assignment operator that we discussed earlier, and you will no longer be comparing the values of the two variables, but instead assign a new value to the variable (this operation will return a true by itself). Not only does this result in a condition that is always true, it can also change the value of the variable we are checking, which can cause many problems.

There is a safe way to prevent this from happening, which is to reverse the position of the variables and constants used to compare them as follows:

if ("Kevin" = = $name) {

The effect is exactly the same, but when you forget the second equals sign, let's see what happens. PHP will attempt to insert the value of the variable ($name) into the constant ("Kevin"). Because you cannot change the value of the constant, PHP interrupts and prompts for an error message, which immediately prompts you to forget the second equals sign!

Conditions can be more complex than whether a single comparison is equal. Recalling our modified welcome.php3, it is possible to acquire both FirstName and LastName at the same time. If we want to display a special message for a particular person, we can compare the two variables at the same time:

if ("Kevin" = = $firstname and "Yank" = = $lastname) {
Echo ("Welcome, OH Glorious leader!");
}

Only the value of $firstname is Kevin, and this condition returns true if the value of $lastname is yank. The meaning of and is that only two comparisons are true, and the whole condition returns TRUE. Another symbol is or, which makes the entire condition return true when at least one of the two comparisons is true. If you have an operator in JavaScript and C (&& (and) and | | | If you are familiar with them, you can use them in PHP as well.

We'll look at some of the more complex comparisons below. Now, we just have to have a basic understanding of the If-else statement.

Another common PHP control statement is a while loop. The If-else statement allows us to choose whether or not to execute a set of statements based on conditions, while loops allow us to determine how many times a set of statements can be repeated based on conditions. The basic format of the while loop should be this:

while ( ) {//As long as the statement is executed repeatedly when it is true}

This is very much like a if-else statement without the ELSE clause. The difference is that when the condition is true, the statement is no longer followed by the end of the execution Terminator (}), but the condition is checked again. If this condition is still true. The statement will be executed repeatedly until the condition is no longer true. When this condition first returns to False (regardless of the number of times it is checked), it jumps to the statement following the execution loop (after the Terminator).

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