This article is about PHP two process judgment statements, there are switch statements and if else, if () statements, let us take a closer look at the example bar.
The code is as follows |
Copy Code |
Switch (variable) { Case value1: Statement1; Break Case value2: ... Default Defulat statement; }
|
The switch statement, based on the value of the variable, is compared with the value in case, and if not equal, continues to find the next
case, if equal, executes the corresponding statement until the switch statement ends or a break is encountered.
The code is as follows |
Copy Code |
Switch ($i) { Case "Apple": echo "I is Apple"; Break Case "Bar": echo "I is bar"; Break Case "Cake": echo "I is cake"; Break } ?> |
Example Two
The code is as follows |
Copy Code |
Switch ($i) { Case 0: echo "I equals 0"; Break Case 1: echo "I equals 1"; Break Case 2: echo "I equals 2"; Break } ?> |
Example Three
The code is as follows |
Copy Code |
Switch ($i) { Case 0: Case 1: Case 2: echo "I was less than 3 but not negative"; Break Case 3: echo "I is 3"; } ?> |
The If...else statement can only select two results: either execute the Truth or perform a false. But now there are more than 2 kinds of options to do
It? At this point, you can use Esleif (or you can write the else if) statement to execute, which is the syntax format:
The code is as follows |
Copy Code |
if (expression1) { Statement1; }else if (expression2) { Statement2; } ... else{ STATEMENTN; }
|
Instance
The code is as follows |
Copy Code |
$moth = Date ("n"); $today = Date ("J"); if ($today >= 1and $today <= 10) { Echo ' Today is '. $moth. ' Month ' $today. ' Early days '; }elseif ($today >10 and $today <=20) { Echo ' Today is '. $moth. ' Month ' $today. ' mid-day '; }else{ Echo ' Today is '. $moth. ' Month ' $today. ' Late in the day '; } ?> |
If the current date is Friday, the following example outputs "a nice weekend!", and if it is Sunday, the output "has a nice sunday!", otherwise the output "has a nice day!" :
The code is as follows |
Copy Code |
$d =date ("D"); if ($d = = "Fri") echo "has a nice weekend!"; ElseIf ($d = = "Sun") echo "has a nice sunday!"; Else echo "has a nice day!"; ?>
|
http://www.bkjia.com/PHPjc/629271.html www.bkjia.com true http://www.bkjia.com/PHPjc/629271.html techarticle This article is about PHP two process judgment statements, there are switch statements and if else, if () statements, let us take a closer look at the example bar. The code below copy code switch (variable) {ca ...