I. Condition Statement
1. if statement Syntax:
If (condition 1) for example $ a = $ B or $ a> $ B
{
Command executed when condition 1 is met, such as echo "a is greater than B ";
}
Elseif (Condition Statement 2) for example, $ a <0 or $ B <0
{
Command executed when condition 2 is met, such as echo "because a or B is less than 0, beyond the optional range ";
}
Else does not need to be connected. The meaning of the Branch is that when neither condition 1 nor condition 2 is met
{
Executed when neither condition 1 nor condition 2 is met. For example, echo "your input is empty. Please enter again ";
}
<? Header ("Content-Type: text/html; charset = UTF-8"); $ a = 100; $ B = 10; if ($ a> $ B) {echo "a> B";} elseif ($ a <$ B) {echo "a <B";} else {echo "a = B" ;}?> Output result a> B
Instance 2
<? Header ("Content-Type: text/html; charset = UTF-8"); $ secore = 92; if ($ secore >=60 & $ secore <70) {echo "your score has passed";} elseif ($ secore >=70 & $ secore <80) {echo "your score is good ";} elseif ($ secore> = 80 & $ secore <90) {echo "your score is good";} elseif ($ secore> = 90 & $ secore <= 100) {echo "your excellent score";} elseif ($ secore> 100 | $ secore <0) {echo "your score is invalid ";} else {echo "your score failed. Please continue to work" ;}?> Output result: your score is excellent.
(2) switch statement syntax
Switch (condition variable) for example: $
{
Case condition variable conditions such as $ a> 0: executed when the conditions are met, such as echo a> 0; break;
Case condition variable conditions such as $ a <0: executed when the condition is met, such as echo a <0; break;
Default: output when none of the preceding conditions are met. For example, echo "a and B are invalid ";
}
<? Header ("Content-Type: text/html; charset = UTF-8"); $ secore = 92; switch ($ secore) {case $ secore> = 60 & $ secore <70: $ pj = "your score has passed"; break; case $ secore> = 70 & $ secore <80: $ pj = "your score is good"; break; case $ secore >=80 & $ secore <90: $ pj = "your score is good"; break; case $ secore> = 90 & $ secore <= 100: $ pj = "your excellent performance"; break; case $ secore <0 | $ secore> 100: $ pj = "your score is invalid"; break; default: $ pj = "your score fails. Continue to work." ;}echo $ pj;?> Output result: your score is excellent.
(3) operator syntax
Echo (condition, for example: $ a> $ B )? If the condition is true, the statement "a> B": if the condition "a <B" is not true ";
<? Header ("Content-Type: text/html; charset = UTF-8"); $ a = 10; $ B = 20; echo ($ a> $ B )? "A> B": "a <B";?> II. Process control statement
Loop statement
1. for statement syntax
For ($ variable name = 0; $ array name <count ($ variable name); $ variable name ++)
{
Echo $ array name [$ variable name];
}
<? Header ("Content-Type: text/html; charset = UTF-8"); $ a = array ('zhang Chao ', 'qiao bin', 'chen wei ', 'zhao Yongfeng '); for ($ I = 0; $ I <count ($ a); $ I ++) {echo $ a [$ I]. "" ;}?> Output result: Zhang Chao, qiao bin, Chen Wei, Zhao Yongfeng
2. while statement syntax
$ Variable name = 0
While ($ variable name <count (array name ))
{
Echo $ array name [$ variable name];
$ Variable name ++
}
<? Header ("Content-Type: text/html; charset = UTF-8"); $ a = array ('zhang Chao ', 'qiao bin', 'chen wei ', 'zhao Yongfeng '); $ I = 0; while ($ I <count ($ a) {echo $ a [$ I]. ""; $ I ++ ;}?> Output result: Zhang Chao, qiao bin, Chen Wei, Zhao Yongfeng
3. foreach loop syntax
Foreach ($ variable name as $ variable rename)
{
Echo $ variable rename;
}
<? Header ("Content-Type: text/html; charset = UTF-8"); $ a = array ('zhang Chao ', 'qiao bin', 'chen wei ', 'zhao Yongfeng '); foreach ($ a as $ v) {echo $ v. "" ;}?> Output result: Zhang Chao, qiao bin, Chen Wei, Zhao Yongfeng
This article is from the "PHP Study Notes" blog