PHP判断和循环的使用

一、Switch判断

if判断,可以做数值区间范围的判断;

switch判断:类似于选择题;在有限的选项内,使用switch会节省代码量;

1、Switch语法

变量:

  • key=value
  • 变量名=变量值
switch (变量key) {
    case '变量值1':
        要执行的代码
        break;
    case '变量值2':
        要执行的代码
        break; 
    case '变量值n':
        要执行的代码
        break; 
}

2、计算器案例

html代码

<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html" charset="utf-8">
        <title>猜数字</title>
    </head>
    <body>
        <form action="test.php" method="get">
            请输入一个值:<input type="text" name="num1"><br />
            请选择运算符:<br />
            <select name="fuhao">
                <option value="">+
                <option value="">-
                <option value="">*
                <option value="">/
            </select><br/>
            请输入一个值:<input type="text" name="num2"><br />
            提交按钮:<input type="submit" value="提交">
        </form>
    </body>
</html>

PHP代码

<?php
    header("content-type:text/html; charset=utf-8");
    $n1 = $_GET['num1'];
    $n2 = $_GET['num2'];
    $fh = $_GET['fuhao'];
    switch($fh){
        case '加':
            echo $n1 + $n2;
            break;
        case '减':
            echo $n1 - $n2;
            break;
        case '乘':
            echo $n1 * $n2;
            break;
        case '除':
            echo $n1 / $n2;
            break;
    }
?>

3、Switch案例

用户输入自己的出生年份,计算出他的属相;
猴、鸡、狗、猪、鼠、牛、虎、兔、龙、蛇、马、羊
0 1 2 3 4 5 6 7 8 9 10 11
html代码

<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html" charset="utf-8">
        <title>猜数字</title>
    </head>
    <body>
        <form action="test.php" method="get">
            请输入您的出生年份:<input type="text" name="year"><br />
            <br />
            提交:<input type="submit" value="提交">
        </form>
    </body>
</html>

PHP代码

<?php
    header("content-type:text/html; charset=utf-8");
    $years = $_GET['year'];
    $res = $years % 12;
    $shuxiang = array("猴","鸡","狗","猪","鼠","牛","虎","兔","龙","蛇","马","羊");
    switch($res){
        case '0':
            echo "根据您的出生年份计算,您的属相为:".$shuxiang[$res];
            break;
        case '1':
            echo "根据您的出生年份计算,您的属相为:".$shuxiang[$res];
            break;
        case '2':
            echo "根据您的出生年份计算,您的属相为:".$shuxiang[$res];
            break;
        case '3':
            echo "根据您的出生年份计算,您的属相为:".$shuxiang[$res];
            break;
        case '4':
            echo "根据您的出生年份计算,您的属相为:".$shuxiang[$res];
            break;
        case '5':
            echo "根据您的出生年份计算,您的属相为:".$shuxiang[$res];
            break;
        case '6':
            echo "根据您的出生年份计算,您的属相为:".$shuxiang[$res];
            break;
        case '7':
            echo "根据您的出生年份计算,您的属相为:".$shuxiang[$res];
            break;
        case '8':
            echo "根据您的出生年份计算,您的属相为:".$shuxiang[$res];
            break;
        case '9':
            echo "根据您的出生年份计算,您的属相为:".$shuxiang[$res];
            break;
        case '10':
            echo "根据您的出生年份计算,您的属相为:".$shuxiang[$res];
            break;
        case '11':
            echo "根据您的出生年份计算,您的属相为:".$shuxiang[$res];
            break; 
    }
?>

二、while/do…while循环

根据条件进行循环;
也可以进行无限循环;
条件判断为真,则循环起来;条件判断为假,则停止循环;

1、while语法

while (条件) {
    循环的代码
}

2、案例:无限循环

<?php
	header("content-type:text/html; charset=utf-8");
	while (1 < 2) {
	    echo "哈哈!";
	}
	?>

3、案例:有限循环

<?php
	header("content-type:text/html; charset=utf-8");
	$i = 1;
	while ($i < 20) {
	    echo "哈哈!";
	    $i++;
	}
?>

4,案例:九九乘法表

<?php
header("content-type:text/html; charset=utf-8");

$num1 = 1;
while($num1 <= 9){
    $num2 = 1;
    while($num2 <= $num1){
        echo $num1.'*'.$num2.'='.$num1*$num2.'&nbsp'.'&nbsp';
        $num2++;
    }
    $num1++;
    echo '<br />';
}
?>

5、do…while

while :必须先满足条件,再循环;
do…while:先执行一次,在判断是否继续执行循环;

语法

<?php
header("content-type:text/html; charset=utf-8");
	do{
	    echo "哈哈";
	}while(1<2)
	
	// while(1>2){
	//     echo "急急如律令!";
	// }
?>

四、for循环

作用:根据条件表达式,进行有限次数的循环;循环一次少一次;

//语法
for (变量的初始值; 循环多少次 ; 自增/自减) { 
    循环的代码
}

1、案例:for循环实现表格案例

<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html" charset="utf-8">
        <title>猜数字</title>
    </head>
    <body>
        <form action="test.php" method="get">
            请输入行数:<input type="text" name="rows"><br />
            请输入列数:<input type="text" name="cols"><br />
            插入表格:<input type="submit" value="插入">
        </form>
    </body>
</html>

PHP代码

<?php
    header("content-type:text/html; charset=utf-8");
    //承接用户的提交表单变量
    $u_row = $_GET['rows'];
    $u_col = $_GET['cols'];
    
    echo '<table border="1">';
    for( $df_row = 1; $df_row <= $u_row; $df_row++ ){
        echo '<tr>';
        for($df_col = 1; $df_col <= $u_col; $df_col++){
            echo '<td>哈哈</td>';
        }
        echo '</tr>';
    }
    echo '</table>';
?>

2、案例:打印表格,实现阶梯效果

打印6行阶梯表格

<?php
    header("content-type:text/html; charset=utf-8");
    //承接用户的提交表单变量
    $u_row = 6;
    
    echo '<table border="1">';
    for( $df_row = 1; $df_row <= $u_row; $df_row++ ){
        echo '<tr>';
        for($df_col = 1; $df_col <= $df_row; $df_col++){
            echo '<td>急急如律令</td>';
        }
        echo '</tr>';
    }
    echo '</table>';
?>

3、案例:for循环打印九九乘法表

<?php
header("content-type:text/html; charset=utf-8");

for($num1 = 1;$num1 <= 9; $num1++){
    for($num2 = 1;$num2 <= $num1;$num2++){
        echo $num1.'*'.$num2.'='.$num1*$num2.'&nbsp'.'&nbsp';
    }
    echo '<br />';
}
?>

五、数组的遍历

<?php
header("content-type:text/html; charset=utf-8");

$shuzu = array(0,1,2,3,4,5,6,7,8,9);
for($i=0; $i <=9 ; $i++){
    echo $shuzu[$i].'<br />';
}
?>

foreach遍历数组

<?php
	header("content-type:text/html; charset=utf-8");
	$shuzu = array("一等奖" => "5w", "二等奖" => "7w","三等奖" => "1亿","四等奖" => "鞋垫一双");
	foreach ($shuzu as $k => $v) {
	    echo "恭喜您获得".$k."喜得".$v.'<br />';
}
?>

六、流程控制中的中断

1,中断的分类

break;     #跳出本层循环
break 2;   #跳出两层循环
continue;  #跳出本次;
exit;      #整个脚本停止;
goto;      #跳转到指定的位置继续执行;

2,break中断

<?php
	header("content-type:text/html; charset=utf-8");
	
	for($num1 = 1;$num1 <= 9; $num1++){
	    for($num2 = 1;$num2 <= $num1;$num2++){
	        $i = $num1 * $num2;
	        if($i % 2 ==0 ){
	            echo $i.'<br />';
	        }
	        break 2;
	    }
	}
	echo "脚本执行结束";
?>

3,continue中断

<?php
	header("content-type:text/html; charset=utf-8");
	
	for($num1 = 1;$num1 <= 9; $num1++){
	    for($num2 = 1;$num2 <= $num1;$num2++){
	        $i = $num1 * $num2;
	        if($i % 2 ==0 ){
	            echo $i.'<br />';
	        }
	        continue;
	    }
	}
	echo "脚本执行结束";
?>

4,exit中断结束脚本

<?php
	header("content-type:text/html; charset=utf-8");
	for($num1 = 1;$num1 <= 9; $num1++){
	    for($num2 = 1;$num2 <= $num1;$num2++){
	        $i = $num1 * $num2;
	        if($i % 2 ==0 ){
	            echo $i.'<br />';
	        }
	        exit;
	        continue;
	    }
	}
	echo "脚本执行结束";
?>

5,goto中断

<?php
	header("content-type:text/html; charset=utf-8");
	goto end;
	for($num1 = 1;$num1 <= 9; $num1++){
	    for($num2 = 1;$num2 <= $num1;$num2++){
	        $i = $num1 * $num2;
	        if($i % 2 ==0 ){
	            echo $i.'<br />';
	        }
	    }
	}
	end:
	echo "脚本执行结束";
?>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值