One: Sequential structure
Two: Branch structure (conditional structure, selection structure)
1. One-way branch:
Conditions: bool,true or false,> < = =!= & | | ! Isset ()
if (condition)
Execute one of the following statements
if (condition)
{
Code Snippets:
Code Snippets:
}
Example:
Code:
$a = 10;
$b = 5;
if ($a > $b)
echo "$a is greater than $b";
2. Two-way branch:
Use Else statement
if (condition)
Execute a statement
Else
Execute a statement
if (condition) {
One or more code
}else{
One or more code
}
Example:
Code:
$a = 10;
$b = 20;
if ($a > $b) {
echo "$a greater than $b<br>";
}else{
echo "$a less than $b<br>";
}
3. Multiplex Branch:
can use if else if and Swich case
It's a mutually exclusive relationship.
if (condition) {
}elseif (condition) {
}elseif (condition) {
}elseif (condition) {
}else{
}
Switch (variable) {
Case Value:
Code:
Break
Case value 1:
Code
Break
Default
}
Example:
Code:
$hour =date ("H");
if ($hour > 6 && $hour < 9) {
echo "Good Morning";
}elseif ($hour > 9 && $hour < 12) {
echo "Good Morning";
}elseif ($hour > && $hour < 14) {
echo "Good noon";
}elseif ($hour > && $hour < 17) {
echo "Good Afternoon";
}elseif ($hour > && $hour < 19) {
echo "Good evening";
}elseif ($hour > && $hour < 22) {
echo "Good evening";
}elseif ($hour > && $hour < 24) {
echo "Good Night";
}else{
echo "Good Morning";
}
The above code can also be written as
$hour =date ("H"); Because there is a mutex, the next piece of code is not executed when the correct code is executed.
if ($hour < 6) {
echo "Good Morning";
}
if ($hour < 9) {
echo "Good Morning";
}elseif ($hour < 12) {
echo "Good Morning";
}elseif ($hour < 14) {
echo "Good noon";
}elseif ($hour < 17) {
echo "Good Afternoon";
}elseif ($hour < 19) {
echo "Good evening";
}elseif ($hour < 22) {
echo "Good evening";
}elseif ($hour < 24) {
echo "Good Night";
}else{
echo "Hello";
}
Example:
Code:
$week =date ("D");
switch (variable) variables here only use integers and strings
Default if a variable does not have a matching value, the region in default is executed
A break is an exit switch statement that sets multiple value matches to execute the same piece of code
Switch ($week) {
Case "Mon":
echo "Monday";
Break
Case "Tue":
echo "Tuesday";
Break
Case "Wed":
echo "Wednesday";
Break
Case "Thu":
echo "Thursday";
Break
Case "Fri":
echo "Friday";
Break
Default
echo "Weekend";
}
If it's a range of judgments, we use ElseIf.
If it's a single value, we use the switch
4. Nested branches:
if () {
if () {
}else{
if () {
}else{
}
}
}else{
if () {
}else{
Switch () {
}
}
}
Example:
Code:
$sex =$_get["Sex"];
$age =$_get["age"];
if ($sex = "man") {
if ($age >= 60) {
echo "The man has retired and he retires." ($age-60). " Years ";
}else{
echo "This man is still working, and". (60-$age). " Years of retirement ";
}
}else{
if ($age >= 55) {
echo "The lady has retired and she retires." ($age-55). " Years ";
}else{
echo "The lady is still working, and". (55-$age). " Years of retirement ";
}
}
The above code is used with get, so the access is the word? sex=man&age=33