Php operators and control structures _ php entry _ script house

Source: Internet
Author: User
Tags switch loop
The operator is a symbol used to perform some operation operations on arrays and variables. If we want to control the structure to effectively correspond to user input, the Code must have the ability to judge. The structure that allows the program to make judgments is called a condition.

The operator is a symbol used to perform some operation operations on arrays and variables. If we want to control the structure to effectively correspond to user input, the Code must have the ability to judge. The structure that allows the program to make judgments is called a condition.

Operators are symbols used to perform some operation operations on arrays and variables.

1. Arithmetic Operators

2. Compound value assignment operator

Increment and decrease operators:

$ A = ++ $ B;

$ A = $ B ++;

$ A = -- $ B;

$ A = $ B --;

3. Comparison Operators

Note: constant equals indicates that only the operands on both sides are equal and the data type is equal.

For example:

0 = "0"

4. logical operators

Operator

5. Ternary Operators

Condition? Value if true: value if false

Example:

6. Error suppression operators:

$ A = @ (57/0 );

The divisor cannot be

7. array Operators

Operator priority and associativity:

Generally, operators have a set of priorities, that is, the order in which they are executed.

Operators are also associated, that is, the execution sequence of operators with the same priority. This sequence is usually left-to-right, right-to-left, or unrelated.

The following table lists the operator priorities. The top operator has the lowest priority, and increases the priority according to the table's ascending order.

Operator priority

To avoid priority confusion, use parentheses to avoid priority.

Control Structure

If we want to effectively input the corresponding user, the Code must have the ability to judge. The structure that allows the program to make judgments is called a condition.

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 if something happens ". 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.
The Code is as follows:
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.
The Code is as follows:
If ($ state = 1 ){
Echo "Haha;
Echo"
";
}
?>

The second method is to add the else condition in addition to the if condition, which can be interpreted as "what to do if something happens, otherwise how to solve it ". Syntax:
If (expr) {statement1} else {statement2}
Example: Modify the preceding example to a more complete process. Because else only executes one line of commands, no braces are added.
The Code is as follows:
If ($ state = 1 ){
Echo "Haha ";
Echo"
";
}
Else {
Echo "Haha ";
Echo"
";
}
?>


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:
The Code is as follows:
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 example uses a for Loop:
The Code is as follows:
For ($ I = 1; $ I <= 10; $ I ++ ){
Echo "This is the first loop". $ I ."
";
}
?>

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.
The Code is as follows:
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.

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.