The second day of ten days to learn php

Source: Internet
Author: User
Tags learn php switch loop

Objective: To master php Process Control

1. if... else loop has three structures

The first is to use the if condition as a simple judgment. It is interpreted as "how to deal with something ". Syntax:

If (expr) {statement}

Expr is the judgment condition, which is usually determined by the logical operator number. Statement is the execution part of the program that meets the conditions. If the program has only one line, you can omit braces {}.

Example: braces are omitted in this example.

<? Php
If ($ state = 1) echo "Haha ";
?>

It is important to note that, to determine whether the equality is equal to = rather than =, ASP programmers may often make this mistake, = is a value assignment.

Example: The execution part of this example has three rows and the braces cannot be omitted.

<? Php
If ($ state = 1 ){
Echo "Haha;
Echo "<br> ";
}
?>


In addition to the if clause, the first two conditions of else can be interpreted as "what to do if something happens, otherwise how to solve it ". Syntax:

If (expr) {statement1} else {statement2} example: the above example is modified to a more complete processing. Because else only executes one line of commands, no braces are added.
<? Php
If ($ state = 1 ){
Echo "Haha ";
Echo "<br> ";
}
Else {
Echo "Haha ";
Echo "<br> ";
}
?>


The third type is the recursive if... else loop, which is usually used for multiple decision making decisions. It combines several if... else values for processing.

Let's look at the example below.

<? Php
If ($ a> $ B ){
Echo "a is larger than B ";
} Elseif ($ a ==$ B ){
Echo "a equals B ";
} Else {
Echo "a is smaller than B ";
}
?>

In the above example, we only use the layer-2 if .. else loop to compare the two variables a and B. Actually, we need to use this recursion if .. please be careful when using else loops, because too many layers of loops may cause problems in the design logic, or if you leave less braces, the program may encounter inexplicable problems.

2. There is only one for loop without any change. Its syntax is as follows:

For (expr1; expr2; expr3) {statement}

Expr1 is the initial value of the condition. Expr2 is the condition for judgment. It is usually determined by a logical operator number (logical operators. Expr3 is the part to be executed after statement is executed. It is used to change the condition for the next loop judgment, such as adding one. Statement is the execution part of the program that meets the conditions. If the program has only one line, you can omit braces {}.

The following is an example of using for loop writing.

<? Php
For ($ I = 1; $ I <= 10; $ I ++ ){
Echo "This is the first loop". $ I. "<br> ";
}
?>

3. switch loop, usually processing compound condition judgment. Each sub-condition is part of the case instruction. In practice, if many similar if commands are used, they can be combined into a switch loop.

Syntax:

Switch (expr) {case expr1: statement1; break; case expr2: statement2; break; default: statementN; break ;}

The expr condition, usually the variable name. The exprN after case usually represents the variable value. The part that meets the condition after the colon. Note that you must use the break to skip the cycle.

<? Php
Switch (date ("D ")){
Case "Mon ":
Echo "today Monday ";
Break;
Case "Tue ":
Echo "Tuesday ";
Break;
Case "Wed ":
Echo "Wednesday ";
Break;
Case "Thu ":
Echo "Thursday ";
Break;
Case "Fri ":
Echo "today's Friday ";
Break;
Default:
Echo "Today's holiday ";
Break;
}
?>

Here, you need to note break; Do not miss it, default. It is acceptable to omit it.

Obviously, it is very troublesome to use the if loop in the above example. Of course, in the design, the conditions with the highest probability should be placed at the beginning, and the minimum conditions should be placed at the end, which can increase the execution efficiency of the program. In the previous example, because the probability of occurrence is the same every day, you do not need to pay attention to the order of conditions.

Let's talk about database usage today.

 

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.