PHP Control Statement 1, if statement
An If statement is an important feature in most languages, and it executes program segments based on conditions. The IF statement in PHP is similar to C:
- if (expr)
- Statement
As discussed in the expression, expr is computed as its true value. If expr is true, PHP executes the corresponding statement and, if False, ignores it.
If $ A is greater than $b, the following example shows ' A is bigger than B ':
- if ($a >$b)
- Print "A is bigger than B";
Typically, you want to execute more than one statement based on the condition. Of course, there is no need to add an IF judgment to each statement. Instead, multiple statements can be formed into a group of statements.
If statements can be nested within other if statements, allowing you to flexibly conditionally execute parts of the program.
PHP Control Statement 2, Else statement
Usually you want to execute a statement when a particular condition is met, and the condition is to execute another statement. else is used to do this. ELSE extended if statement to execute another statement when the IF statement expression is false. For example, the following program executes if $a is greater than $b displays ' A is bigger than B ', otherwise display ' A is '
- Not bigger than B ':
- if ($a>$b) {
- Print "A is bigger than B";
- }
- else {
- Print "A is not bigger than B";
- }
PHP Control Statement 3, ElseIf statement
ELSEIF, as the name implies, is a combination of if and else, similar to else, that extends the IF statement to execute other statements when the IF expression is false. However, unlike else, it executes other statements only when the ElseIf expression is true.
You can use more than one ElseIf statement in an if statement. The first statement that ElseIf expression is true will be executed. In PHP 3, you can also write ' else if ' (written in two words) and ' ElseIf ' (written as a word) effect. This is just a small difference in writing (if you're familiar with C, it's also) and the result is exactly the same.
The ElseIf statement is executed only if the IF expression and any preceding ElseIf expression are false, and the current ElseIf expression is true.
The following is a nested-format if statement that contains ElseIf and else:
- if ($a==5):
- Print "a equals 5";
- Print "...";
- ElseIf ($a==6):
- Print "a equals 6";
- print "!!!";
- Else
- Print "A is neither 5 nor 6";
- endif
PHP Control Statement 4, while statement
The while loop is a simple loop for PHP 3. The same as in C. The basic format of the while statement is:
while (expr) statement
The meaning of the while statement is very simple. It tells PHP to repeatedly execute nested statements as long as the while expression is true. The value of the while expression is checked each time the loop starts, so even if it changes its value within the nested statement, the execution does not terminate until the loop ends (each time PHP runs a nested statement called a loop). Similar to the IF statement, you can enclose a set of statements in curly braces and execute multiple statements in the same while loop:
while (expr): statement ... Endwhile;
The following example is exactly the same, with the numbers 1 to 10:
/* Example 1 */
- $ I = 1 ;
The above loop executes only once, because after the first loop, when the truth expression is checked, it calculates that it is FALSE ($i not greater than 0) loop execution terminates.
PHP Control Statement 5, for Loop statement
The For loop is the most complex loop in PHP. The same as in C. The syntax for the For loop is:
for (EXPR1; expr2; expr3) statement
The first expression (EXPR1) is evaluated unconditionally (executed) at the beginning of the loop.
Each time the loop, the expression expr2 is computed. If the result is TRUE, the loop and nested statements continue to execute. If the result is FALSE, the entire loop ends.
At the end of each cycle, the EXPR3 is computed (executed). Each expression can be empty. Expr2 is empty, the number of cycles is variable (PHP defaults to True, like C). Don't do this unless you want to end the loop with a conditional break statement instead of a for truth expression.
Consider the following example. They all show numbers 1 through 10:
/* Example 1 */
- for ($ i = 1 ; $i) {
- break;
- }
- print $i;
- }
- /* Example 3 */
- $ i = 1 ;
- for (;;) {
- if ($i > ) {
- break;
- }
- print $i;
- $i + +;
- }
Of course, the first example is obviously the best, but you can see that you can use an empty expression for many occasions in the For loop.
Other languages have a foreach statement to traverse an array or hash table. PHP uses the While statement and the list (), each () function to achieve this function.
PHP Control Statement 6, switch SELECT statement
A switch statement is like a series of if statements on the same expression. In many ways, you want to compare the same variable (or expression) with many different values and execute different program segments based on different comparisons. This is the use of the switch statement.
The following two examples do the same thing in different ways, one with a set of if statements, and the other with a SWITCH statement:
/* Example 1 */
- if ($i = = 0) {
- print "I equals 0";
- }
- if ($i = = 1) {
- Print "I equals 1";
- }
- if ($i = = 2) {
- Print "I equals 2";
- }
- /* Example 2 */
- Switch ($i) {
- Case 0:
- print "I equals 0";
- Break
- Case 1:
- Print "I equals 1";
- Break
- Case 2:
- Print "I equals 2";
- Break
- }
http://www.bkjia.com/PHPjc/445943.html www.bkjia.com true http://www.bkjia.com/PHPjc/445943.html techarticle PHP Control Statement 1, if statement if statement is an important feature in most languages, it executes the program segment according to the condition. The IF statement in PHP is similar to c:if (expr) statement as discussed in the expression ...