Home >Backend Development >PHP Tutorial >php if else swicth conditional control statement study notes_PHP tutorial
The most commonly used conditional control statements in PHP are if else or switch statements. Now I will give you a detailed introduction to my notes on using if else conditional control statements in PHP. Friends in need can refer to them.
In the PHP language, the basic process control structures are: sequential structure, branch structure, and loop structure.
Commonly used control statements mainly include:
if, if...else judgment statements
switch branch statement
while, do...while loop statement
for loop statement
break, continue interrupt statements
The php if statement is a simple judgment statement, which implements the function of conditional judgment. When the program executes a statement and encounters a fork in the road, it selects and executes the corresponding statement by judging whether the value of the statement meets the conditions. So the if statement is the most basic PHP conditional expression.
The basic structure is:
if (judgment statement)
{
Execution statement body
}
Example:
The code is as follows | Copy code | ||||
if($a>$b){ /* If $a>$b, execute the execution statement within the curly brackets */ $a++; } $c=$a+$b;echo "a+b=".$c; ?>
|
If the value is non-0 (i.e. true), then statement body 1 is executed, and if the value is 0 (i.e. false), then statement body 2 is executed.
The basic structure of the if...else statement is:
if (judgment statement)
{
Execute statement body 1
}
else
代码如下 | 复制代码 |
$a=11; |
{
Execute statement body 2
}
Example:
The code is as follows | Copy code |
$a=11; $b=9; if($a>$b){$c=$a+$b; }else{ $c=$a-$b;} echo $c;?> |
代码如下 | 复制代码 |
$score=61; |
The code is as follows | Copy code |
$score=61;<🎜> if($score >=90){ echo "Excellent results"; } else if($score >=60){ echo "passed."; } else if($score <60){<🎜> Failed;<🎜> }<🎜> else echo "Wrong results";<🎜> ?> |
Multiple if() statements can be nested in the if statement to realize the judgment of multiple parameters. It is generally called multiple nesting of if statements. Its basic structural form is as follows:
if(judgment 1)
if(judgment 2) statement body 1
else statement body 2
else
…
You should pay attention to the pairing relationship between if and else here. Starting from the inner layer, else is always paired with the nearest if above it. Be particularly careful when programming.
Example:
The code is as follows
|
Copy code
|
||||||||
$gender="female";
$age=28;
if($gender=="male"){ } }
The value of the expression is first matched with the case statement one by one. If there is a match, the statement body is executed, and then the loop is jumped out. If no case statement is matched in the end, the execution statement after default is executed.
Example:
Statement: The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn Previous article:Study notes on for while loop statement in php_PHP tutorialNext article:Study notes on for while loop statement in php_PHP tutorial Related articlesSee more |