Table of Contents [1] Conditional statement IF statement switch statement [2] loop statement while loop Do...while loop for loop foreach Loop condition statement
For performing different actions based on different conditions
If statement
if code executed when ( condition) {true ;}
if (condition) { trueElse { false when executing code;}
if (condition) {condition = true true code executed;} else false
<? PHP $t=date("H"); if ($t< "ten") { echo "has a good morning!" ElseIf ($t< ") {echo " has a good day! " Else { echo "has a good night!" ;}? >
Switch statement
switch case label1: code to be executed if expression = Label1; break ; case label2: code to be executed Span style= "color: #0000ff;" >if expression = Label2; break default : code to be executed if expression is different from both Label1 and Label2;}
<? php switch ( $x 1: echo "Number 1" ; break case 2: echo "Number 2" ; break case 3: echo "Number 3" ; break default : echo "No number between 1 and 3" Span style= "color: #000000;" >;}?
Looping statements
When writing code, you often need to run the same block of code repeatedly, and you can use loops to perform such tasks
While loop
while {The condition is true) { the code to execute;}
<?PHP$sum= 12;//the current level of hunger for small petsEcho"I'm hungry,:-(.";Echo"<br/>"; while($sum<100){//the small pet's hunger level to 100, indicates that the small pet eats the full, does not have to continue to feed, does not have the satiety to continue to feed $num=Rand(1,20);//random numbers, simulating small-pet-fed buns $sum=$sum+$num;//Small pets eat small bread Echo"I haven't eaten enough yet!" "; Echo"<br/>";}Echo"I'm finally fed up, ^_^".;?>
Do...while Cycle
The loop executes the code block first, then checks the condition and repeats the loop if the specified condition is true
Do { while (condition is true);
<? PHP $sum = 0; Do { $numrand(1,6); Gets the random number from 1 to 6, simulates the dice $sum$sum $num; forward step }while ($num==6); Echo "The Do...while example executes and goes forward:". $sum . " <br/> ";? >
For loop
For loop statements, initialization is evaluated unconditionally before the start of the loop, and the loop condition is evaluated before each cycle begins. If the value is true, the loop is resumed, the loop body statement is executed, and if the value is False, the loop is terminated. Increment statements are executed after each loop
for (init counter; test counter; increment counter) { code to be executed;}
<? for ($x$x$x+ +) {echo "number is:$x <br> "?>
foreach Loop
The Foreach loop applies only to arrays, which iterate through each key/value pair in the array. For each iteration of the loop, the value of the current array element is assigned to the $value variable, and the array pointer moves one at a time until the last array element is reached. Generally there are two ways: Do not remove the label, remove the
[1] Only value, do not remove the mark
foreach ($arrayas$value) {code to be executed;}
<? $colorsarray("Red", "green", "blue", "yellow"foreach ($ Colorsas$value) { echo "$value <br>" ;}? >
[2] Simultaneously remove the mark and value
foreach ( $array as $index = $value ) {code to be executed;}
<? $colorsarray( "r" = "Red", "g" = "green", "b" = "Blue", "Y" = "yellow"foreach ($colorsas$key$value { echo$key. ":". ) $value. " <br> ";}? >
Front-end learning of PHP statements