PHP 循环

1585364631
2022-03-10 / 0 评论 / 172 阅读 / 正在检测是否收录...

PHP 循环

PHP while循环输出金字塔

<?php
    $i = 1;
    $line = 10;
    while($i<=$line){
        $j = $line - $i;
        $k = 2 * $i - 1;
        while($j>0){
            $j--;
            echo "&nbsp";
        }
        while($k>0){
            $k--;
            echo "*";
        }
        echo "<br />";
        $i++;
    }
?>

PHP for循环输出九九乘法表

<?php
    echo ("<style>td{border:2px solid black;text-align:center;padding:5px;}</style><table>");
    for($i=1;$i<=9;$i++){
        for($ii=1;$ii<=$i;$ii++){
            echo ("<td>" . $ii . "x" . $i . "=" . $i*$ii . "</td>");
        }
        echo ("<tr/>");
    }
    echo ("</table>")
?>

PHP forearm

遍历数字索引数组所有值

<?php
    $sz = array(1,2,3,4,5,6,7,8,9);
    foreach($sz as $i){
        echo $i . "<br/>";
    }
    
    foreach($sz as $i => $k){
        echo "索引:" . $i . "    值:" . $k . "<br/>";
    }
?>

遍历字符关联数组(键值对)

<?php
$key_value0 = array('name'=>'手机','num'=>'5','price'=>'4500');
    foreach($key_value0 as $i){
        echo "$i ";
    }
    echo ('<br>');
    foreach($key_value0 as $i => $j){
        echo "键名:$i 键值:$j <br/>";
    }

    $key_value = array(
        array('name'=>'手机','num'=>'5','price'=>'4500'),
        array('name'=>'电脑','num'=>'15','price'=>'10500'),
        array('name'=>'鼠标','num'=>'500','price'=>'20'),
        array('name'=>'键盘','num'=>'500','price'=>'20')
    );
    
    foreach($key_value as $i){
        foreach($i as $k => $v){
            echo $k . '----' . $v . ' ';
        }
        echo '<br/>';
    }

     foreach($key_value as $ii){
        echo '物品:' . $ii['name'] . ' 数量:' . $ii['num']  . ' 价格:' . $ii['price'] . "<br/>";
    }

 ?>
0

评论 (0)

取消