Conditional structure statements in PHP

Source: Internet
Author: User
In the PHP development process, if we want to effectively respond to user input, the code needs to be judged. The structure that allows the program to make judgments is called a condition. What are the condition structure statements in PHP?

In the PHP development process, if we want to effectively respond to user input, the code needs to be judged. The structure that allows the program to make judgments is called a condition. What are the condition structure statements in PHP? What is the specific syntax? In which part of the application? Read the following carefully!

1. if statement

The if statement can be understood as follows. In PHP, we can use the if statement for logical judgment, but the conditions for using the if statement must be given. If the condition is true, the subsequent code block is executed. The conditions of the if statement must be enclosed in parentheses. The basic structure and example of the if statement are as follows:
Basic structure: if (condition) {code block}
Statement instance:
If ($ today = Sunday ){
Echo"

";
Echo "today is not going to work! ";
}

In the above example, we use the condition $ today = Sunday. Remember that the equal operator (=) is different from the value assignment operator (=. If $ today is equal to Sunday, the condition $ today = Sunday is true, and the echo statement is executed. if the condition is not true, the conditional expression returns false, the echo statement will not be executed. Generally, depending on the action of an if statement, we may want to execute multiple statements. We can put multiple statements together to form a code block. To declare a code block, enclose it with braces.

2. else statements

Generally, you need to judge not only the actions you want to execute, but also a series of actions that may be executed. Here we need to use the else statement, which is generally used together with the if statement. When the if statement returns false, the else statement allows us to define an action to replace. Let's take a look at the following example.
If ($ today = Sunday ){
Echo"

";
Echo "today is not going to work! ";
} Else {
Echo "it's annoying. it's coming to work today! ";
}

Of course, this is only a simple judgment. to process more complex logic judgment, you need to use nested if statements. In the following example, the prompt information should be displayed not only when the if condition $ today = Sunday is true, but also when each condition is true.
If ($ today = Sunday ){
Echo"

";
Echo "today is not going to work! ";
} Else {
If ($ time = "9:00 ")
Echo "it's time to get up! ";
If ($ today = "Saturday ")
Echo "the weekend is over! ";
}

3. elseif statement

When you need to make multiple decisions, these decisions may have more than two options. We can use the elseif statement to create a multi-choice sequence. The elseif statement is a combination of else and if statements. By providing a series of conditions, the program checks each condition until it finds a condition of true. For example, a merchant provides a certain discount for customers with large orders. The discount scheme is as follows.
Purchase less than 10: No discount
Purchase in 10 ~ 49: 5% discount
Purchase in 50 ~ 99: 10% discount
More than 100 purchases: 15% discount
You can use conditional expressions and the if and elseif statements to compile the discount calculation code. In this example, the "and" operator (&) must be used to combine the two conditions into one. the specific implementation code is as follows (assume that the total number of customer orders is $ oderqty and the discount is $ discount ):
If ($ oderqty <10 ){
$ Discount = 0;
} Elseif ($ oderqty >=10) & ($ oderqty <= 49 )){
$ Discount = 5;
} Elseif ($ oderqty >=50) & ($ oderqty <= 99 )){
$ Discount = 10;
} Elseif ($ oderqty >=100 ){
$ Discount = 15;
}

Note that elseif statements can be written as spaces in the middle of elseif or else if.

4. switch statement

The switch statement is similar to the if statement, but it can have more than two possible values. In an if statement, the condition can only be true or false. In a switch, as long as the condition value is a simple data type (integer, string, or Rune type), the condition can have any number of different values. The switch statement must provide a case statement to process each condition value and provide the corresponding action code. In addition, there should be a default case condition to handle situations where no specific value is provided. For example, a merchant wants to know the way in which the customer generates transactions. In this case, you can add an investigation question to the order submission page and add the following HTML code to the order submission form.

How do you know the website?
-- Select --Friend introductionTV adsOnline advertisementSearch engine

In the preceding example, a new form variable (variable name: find) is added to the HTML code. its values can be a, B, c, and d. Here we can use the switch statement to process the data submitted by the form. The code is as follows:
$ Find = $ _ POST ['find '];
Switch ($ find ){
Case "":
Echo"

Friend introduction

";
Break;
Case "B ":
Echo"

TV ads

";
Break;
Case "c ":
Echo"

Online advertisement

";
Break;
Case "d ":
Echo"

Search engine

";
Break;
Default:
Echo"

I don't know where to know from.

";
Break;
}
?>

The switch statement and the if or elseif statement have different behaviors. If no special curly braces are used to declare a statement block, the if statement can only affect one statement. The switch statement is the opposite. When a specific case in the switch statement is matched, PHP will execute the code in the case until the break statement is encountered. If there is no break statement, switch will execute all the code in the case with the value true below this case. When a break statement is encountered, the statement after the switch is executed.

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.