conditional control statements mainly have if, If...else, ElseIf and switch4 species. ElseIf, as implied by this name, is the combination of if and else. As with else, it extends the IF statement and can execute a different statement if the original if expression has a value of FALSE. However, unlike else, it only executes the statement when the conditional expression value of ElseIf is true.
If statement
Almost all programming languages have an if statement, which executes different snippets of code according to the criteria chosen. The format of the IF statement for PHP is:
if (expr) statement;
If the expression expr value is true, then the statement statement is executed sequentially, otherwise the statement is skipped and then executed, and if more than one statement needs to be executed, you can use "{}", and the statement in "{}" is called a statement group in the form:
if (expr) { statement1; Statement2; ...}
<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
Operation Result:
$num =16
16 is even
If...else statements
In most cases, it is always necessary to execute a statement when a condition is met, and to execute other statements when the condition is not met. This is possible using the If...else statement, which is in the syntax format:
if (expr) { statement1;} else{ Statement2;}
The statement means: When the expression expr is true, statement1 is executed and statement2 is executed if the expression expr is false.
<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "
Run Result: variable 5 is an odd number
ElseIf statements
The If...else statement can only select two results: either execute the Truth or perform a false. However, there are sometimes more than two choices, which can be performed using the ElseIf statement, which is in the following syntax:
if (expr1) { statement1;} else if (EXPR2) {}...else{ statementn;}
<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
Switch...case Multiple Judgment statements
Although the ElseIf statement makes multiple selections, it is cumbersome to use. To prevent the if statement from being too verbose and providing readability for the program, you can use the switch multiple judgment statement. The syntax format is as follows:
Switch (variable) {case value1: statement1; break; Case value2: ... Default: default statement;}
The switch statement is based on the value of the variable, and, in turn, compares the value values in the case, and if not equal, continues to find the next case, and if it is equal, executes the corresponding statement until the switch statement ends or a break is encountered. In general, the switch statement eventually has a default value, and if a matching condition is not found in the previous case, the statement after the default is output similar to the Else statement.
<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "
Output result: This is a 1