Basic grammatical structure of if single branch
if (conditional expression) {
Execute the statement;
}
If Double Branch
Basic grammatical structure
if (conditional expression) {
Execute the statement;
}else{
Execute the statement;
}
If multiple branches
The basic grammatical structure is
if (conditional expression) {
Execute the statement;
}else if (conditional expression) {
Execute the statement;
}else if (conditional expression) {
Execute the statement;
}else{//there may be more else if
Execute the statement;
}
☞ to the structure above, please note:
1 else if can have one, or can have multiple
2) Else can not
Cases
The code is as follows |
Copy Code |
<form method=post action= "" > Account Number: <input type= "text" name= "accounts" ><BR> Password: <input type= "text" name= "password" ><BR> <input type= "Submit" value= "Send Out" > </FORM>
<?php if ($account = = "Lord" && $password = "Pass") { echo "Welcome $_post[account], your password is $_post[password]"; }elseif ($_post[account] = = "God" && $_post[password] = = "Dog") { $y = Date ("Y")-1911; $m = Date ("M"); $d = Date ("D"); echo "Welcome $_post[account", today is Republic ". $y." Year ". $m." Month ". $d." Day "; }else{ echo "Login failed:} ?> |
Switch Branch Statement
Basic grammatical structure
switch (expression) {
CASE constant 1:
Execute the statement;
Break
Case constant 2:
Execute the statement;
Break
Defual:
Execute the statement;
Break
}
Cases
The following two examples implement the same thing in different ways, the first is to use a if...elseif...else statement, and a switch statement.
The code is as follows |
Copy Code |
<?PHPIF ($i = = 0) {echo "I equals 0";} elseif ($i = = 1) {echo "I equals 1";} elseif ($i = = 2) {echo "I Equa ls 2 ";} else { echo "I am not equal to 0, 1 or 2"; } ?>
|
The above PHP code means: If the variable $i equals 0, the output "I equals 0" is output "I equals 1" if the variable $i equal to 1, or "I equals 2" if the variable $i equals 2; If none, output "I is n" OT equal to 0, 1 or 2 ".
Comparison of switch statements and ElseIf statements
In a switch statement, the condition is only once, then compared to each case, and in the ElseIf statement, the condition is evaluated again. If your condition is more complex, or multiple loops, the switch statement will be faster.
The code is as follows |
Copy Code |
Isempty= ""; $isEmpty = "NOT NULL"; $test = Empty ($isEmpty)? ": Trim ($isEmpty); |
The three-mesh operator?: Empty ($isEmpty) True or false, based on the preceding criteria, evaluates to the following expression, false when the condition is true: the following expression.
If you are not accustomed to change to the following code
The code is as follows |
Copy Code |
if (empty ($isEmpty))//condition set up { $cookie = ""; } Else { $cookie = Trim ($isEmpty); }
|
☞ Please note:
1 Case statement has one or more
2 Defaul statement can not (according to the business logic of its own code)
3 Usually, after the case statement, you want to take a break, which means exit the switch statement
4 The type of the constant (int, float, string, Boolean)
Supplements
comparison operator.
$a = = $b equal to TRUE if $a equals $b.
$a!= $b Unequal TRUE If $a is not equal to $b.
$a <> $b is not equal to TRUE if $a is not equivalent to $b.
$a < $b small and TRUE if $a are strictly smaller than $b.
$a > $b is greater than TRUE if $a strictly $b.
$a <= $b is less than or equal to TRUE if $a is less than or equal to $b.
$a >= $b greater than or equal to TRUE if $a is greater than or equal to $b.
logical operators.
$a and $b and (logical AND) true if $a and $b are true.
$a or $b or (logical OR) true if either $a or $b is true.
$a XOR $b xor (Logical XOR OR) True if either $a or $b is true, but not at the same time.
! $a not (logical not) True if the $a is not true.
$a && $b and (logical AND) true if $a and $b are true.
$a | | $b or (logical OR) true if either $a or $b is true.