This article describes two php process judgment statements: switch statement and if else and if () statement. Let's take a look at the example in detail.
| The Code is as follows: |
Copy code |
Switch (variable ){
Case value1:
Statement1;
Break;
Case value2:
...
Default:
Defulat statement;
}
|
The switch statement is compared with the value in case based on the value of variable. If the value is not equal, continue to find the next
Case; if they are equal, execute the corresponding statement until the switch statement ends or the break is met.
| The Code is as follows: |
Copy code |
<? Php
Switch ($ I ){
Case "apple ":
Echo "I is apple ";
Break;
Case "bar ":
Echo "I is bar ";
Break;
Case "cake ":
Echo "I is cake ";
Break;
}
?> |
Instance 2
| The Code is as follows: |
Copy code |
|
<? Php
Switch ($ I ){
Case 0:
Echo "I equals 0 ";
Break;
Case 1:
Echo "I equals 1 ";
Break;
Case 2:
Echo "I equals 2 ";
Break;
}
?> |
Instance 3
| The Code is as follows: |
Copy code |
|
<? Php
Switch ($ I ){
Case 0:
Case 1:
Case 2:
Echo "I is less than 3 but not negative ";
Break;
Case 3:
Echo "I is 3 ";
}
?> |
If... Else statements can only select two types of results: true or false. But now there are two or more options. What should I do?
What about it? In this case, you can run the esleif (or write else if) Statement in the following 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 |
<? Php
$ Moth = date ("n ");
$ Today = date ("j ");
If ($ today> = 1and $ today <= 10 ){
Echo 'Today is '. $ moth. 'month'. $ today. 'day-to-day ';
} Elseif ($ today> 10 and $ today <= 20 ){
Echo 'Today is '. $ moth. 'month'. $ today.' mid-day ';
} Else {
Echo 'Today is '. $ moth. 'month'. $ today.' late Thursday ';
}
?> |
If the current date is Friday, the following example will output "Have a nice weekend! ", If it is Sunday, output" Have a nice Sunday! ", Otherwise output" Have a nice day! ":
| The Code is as follows: |
Copy code |
|
<Html>
<Body>
<? Php
$ D = date ("D ");
If ($ d = "Fri ")
Echo "Have a nice weekend! ";
Elseif ($ d = "Sun ")
Echo "Have a nice Sunday! ";
Else
Echo "Have a nice day! ";
?>
</Body>
</Html> |