PHP流程控制语句-while、do..while循环语句

本文通过两个具体的PHP示例,详细解析了while和do-while循环的使用方式及区别。通过输出指定范围内的偶数和根据条件重复执行代码块,展示了不同循环结构的特点。
<?php

    function cover_EvenNumbers($obj){
        $i=1;
        while ($i <= $obj) {
            if ($i % 2 ==0) {
                echo "$i ";
            }
            $i++;
        }
    }

    cover_EvenNumbers(100);
?>

do while和while的区别是,do里面的那个100%会跑。。。

<?php

    function doWhile_Test($obj){
        $i=1;
        while ($obj ==2) {
            if($i==25){
                return;
            }
            echo "the obj is 2!!<br>";
            $i++;
        }
        do {
            echo "the obj is $obj<br>";
        } while ($obj ==2);//while 循环里面写的要和下面do-while匹配上
    }

    doWhile_Test(2);
?>
the obj is 2!!
the obj is 2!!
the obj is 2!!
the obj is 2!!
the obj is 2!!
the obj is 2!!
the obj is 2!!
the obj is 2!!
the obj is 2!!
the obj is 2!!
the obj is 2!!
the obj is 2!!
the obj is 2!!
the obj is 2!!
the obj is 2!!
the obj is 2!!
the obj is 2!!
the obj is 2!!
the obj is 2!!
the obj is 2!!
the obj is 2!!
the obj is 2!!
the obj is 2!!
the obj is 2!!
the obj is 2!!
the obj is 2!!
the obj is 2!!
the obj is 2!!
the obj is 2!!
the obj is 2!!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值