PHP statement Directory for the front end: [1] condition statement if statement switch statement [2] loop statement while loop do & hellip; while loop for loop foreach loop condition statement is used to execute different actions based on different conditions if statement if (condition) {code executed when the condition is true;} if (condition) {code executed when the condition is true;} else {the condition is the PHP statement learned by the f frontend
Directory [1] condition statement if statement switch statement [2] loop statement while loop do... While loop for loop foreach loop
Condition Statement
Used to execute different actions based on different conditions
If statement
If (condition) {code executed when the condition is true ;}
If (condition) {code executed when the condition is true;} else {code executed when the condition is false ;}
If (condition) {code executed when the condition is true;} elseif (condition) {code executed when the condition is true;} else {code executed when the condition is false ;}
Switch statement
switch (expression){case label1: code to be executed if expression = label1; break; case label2: code to be executed if expression = label2; break;default: code to be executed if expression is different from both label1 and label2;}
Loop statement
When writing code, you often need to run the same code block repeatedly. you can use loops to execute such tasks.
While loop
While (condition true) {code to be executed ;}
"; While ($ sum <100) {// The hunger level of a small pet reaches 100, indicating that the small pet is full and does not need to continue feeding, don't eat continue feeding $ num = rand (); // random number, simulate feeding small pet bread $ sum = $ sum + $ num; // small pets eat bread echo "I'm not full yet! "; Echo"
";}Echo" finally enough ^_^ ";?>
Do... While loop
Loop first executes a code block and then checks the condition. if the specified condition is true, the loop is repeated.
Do {code to be executed;} while (the condition is true );
";?>
For loop
In a for loop statement, the value is obtained unconditionally before the start of the loop, and the loop condition is evaluated before the start of each loop. If the value is true, the loop continues and the loop body statement is executed. if the value is false, the loop ends. The incremental statement is executed after each loop.
for (init counter; test counter; increment counter) { code to be executed;}
";}?>
Foreach loop
The foreach loop is only applicable to arrays and is used to traverse each key/value pair in the array. Every iteration of an array, the value of the current array element is assigned to the $ value variable, and the array pointer is moved one by one until the last array element is reached. There are two methods: No subscript and no subscript
[1] only values, not subscript
foreach ($array as $value) { code to be executed;}
";}?>
[2] getting both subscript and value
foreach ($array as $index => $value) { code to be executed;}
"red", "g"=>"green", "b"=>"blue", "y"=>"yellow"); foreach ($colors as $key => $value) { echo $key.":".$value."
";}?>
-
Wen haoxue PHP on the first floor
-
PHP is the first programming language that the landlord came into contact?
-
Re: the blue ideal of a match
-
@ 文 PHP: I am a beginner. I only need to know about js and PHP.