PHP process control statement

PHP process control statement

Divided into the following four categories:
1. Sequential statement programming
2. Separate statement programming
3. Loop statement programming
4. Mixed programming

1. Sequential structure
program to achieve two integer swap positions

<?php
	/*定义两个变量*/
	$i = 9; 
	$j = 10; 
    echo "交换前:i = $i j = $j<br>";
	$tmp = $i; 
	$i = $j ;
	$j = $tmp;
	echo "交换后:i = {
      
      $i} j = {
      
      $j}";
?>

The results of the operation are as follows:
Insert picture description here
2. Branch structure:
According to the grade of a student, the grade of his grade is determined.

<?php
/*定义成绩变量*/
$score = 95;
if($score >= 90){
    
    
    echo"优";
}elseif ($score >=80){
    
    
	echo "良";
} else if($score >=70){
    
    
	echo "中";
} elseif($score >=60 ){
    
    
	echo "及格";
} else {
    
    
	echo "不及格";
}
?>

Running results:
Insert picture description here
3. Loop structure
Print all prime numbers within 100

<?php
//求100以内素数
for ($i = 1; $i <= 100; $i++) {
    
    
    $k = 0;
    for ($j = 1; $j < $i; $j++) {
    
    
        if ($i % $j == 0) {
    
    
            $k++;
        }
    }
    if ($k == 1) {
    
    
        echo $i;
        echo "<br>";
    }
}
?>

Running results:
Insert picture description here
4. Nested programming
Use nested programming to print 9*9 forms

<table border ='1'>
	<?php  for($i = 1; $i <= 9; $i++):?>
	<tr>
		<?php for($j = 1; $j <= $i; $j++):?>
		<td><?=$i?> * <?=$j?> = <?=$i*$j?></td>
		<?php endfor;?>
	</tr>
	<?php endfor;?>
</table>

operation result:
Insert picture description here

Well, the above are the four ways of PHP process control statements. I hope to help you!

Guess you like

Origin blog.csdn.net/qq_44023710/article/details/113172711