Control structure emphasis in this chapter ◆ establish and test the combined logic ◆ Use if and switch for branch processing ◆ Use while and for ◆ Use exit and die to exit the page for execution if the program cannot be executed due to different situations to determine different executions, the structure cannot be controlled.
Highlights of this Chapter
◆ Establish and test the combination logic
◆ Use if and switch for branch processing
◆ Use while and
◆ Use exit and die to exit the execution of the page
It is not easy to compile useful programs if the program cannot be determined based on different situations. Simply put, the code behavior of the output variable depends on the value of a variable. as a programmer, we can use different actions to allow programs to respond differently to events (in combination with external world, time, user input, or database content ).
This type of program response requires a control structure that controls the execution of code in different cases. In the previous chapter, we used the if control structure, but did not really explain it in depth. in this chapter, we will introduce every control structure provided by PHP, and study their operation and operation methods in detail.
For experienced C language programmers, "control" is the most similar to the C language style in all PHP functions, all the structures used in the C language can be used here, and the operation method is the same. If you are an experienced C language programmer, you can skip the section at the end of this chapter.
The two major control structures we will discuss are branch and loop ). A branch is a sub-interface on the program execution path. depending on a test poem, the program can be left or right. the path may be different in the future, or it may be merged again. Loop is one of the branch types. it has an execution path that is switched back to the beginning of the branch. it can be re-covered for testing and may be re-overwritten for loop execution.
Before using the control structure effectively, you must be able to effectively construct test conditions. Let's start with the simplest test, first understand the constants TRUE and FALSE, and then use these tests in more complex code.
Boolean formula
Each control structure introduced in this chapter contains two completely different parts: one is the test part (which of the following directions is determined ), A test is performed by testing the code (for a separate branch or loop) through the Boolean operation, the result is calculated based on "true" or "non-true.
Boolean constant
The simplest type of operation is a simple value. The simplest Boolean values are the TRUE and FALSE constants, and vice versa. For example, we can embed them in the test section described in if-else:
If (TRUE) print ("This will always print"); elseprint ("This will never print"); the above example is the same as that described below: if (FALSE) print ("This will never print"); elseprint ("This will always print ");
Logical operators
Logical operators can combine other logical values (also called Boolean) to generate new Boolean values. PHP supports the first two alternative versions of standard logical operations (and, or, not, and xor), as shown in Table 7-1.
7-1 logical operator number
The above is the content in Chapter 7 of the PHP Learning Guide. For more information, see The PHP Chinese website (www.php1.cn )!