douduxia1551 2016-10-27 16:11
浏览 64
已采纳

如何在php函数中使用for循环

<?php
    function iterate($x){ //this is the function
        for ($i = 0; $i <= 10; $i++){ //this is the loop procedure to iterate 10 times
            echo $i; //it will show 0123456789 on the screen
        } 
    }

    $y = "xyz"; //variable declaration
    echo iterate($y); //should be iterate xyz as much 10 times. 
?>

wish to echo(print) xyz ten times using for loop inside the php function, the result not as expected. how to show the xyz iterate ten times.

  • 写回答

4条回答 默认 最新

  • dongpu3347 2016-10-27 16:12
    关注

    echo $x; which is the value you pass to the function. You do not have to echo the function because echo is called inside the function. You also need to change your counter. 0 to 9 is 10 times, or 1 to 10.

    function iterate($x){ //this is the function
            for ($i = 0; $i <= 9; $i++){ //this is the loop procedure to iterate 10 times
                echo $x; //it will show xyz on the screen, 10 times
            } 
        }
    
        $y = "xyz"; //variable declaration
        iterate($y); //should be iterate xyz as much 10 times. 
    

    EXAMPLE

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 学不会递归,理解不了汉诺塔参数变化
  • ¥15 基于图神经网络的COVID-19药物筛选研究
  • ¥30 软件自定义无线电该怎样使用
  • ¥15 R语言mediation包做中介分析,直接效应和间接效应都很小,为什么?
  • ¥15 Jenkins+k8s部署slave节点offline
  • ¥15 如何实现从tello无人机上获取实时传输的视频流,然后将获取的视频通过yolov5进行检测
  • ¥15 WPF使用Canvas绘制矢量图问题
  • ¥15 用三极管设计一个单管共射放大电路
  • ¥15 孟德尔随机化r语言运行问题
  • ¥15 pyinstaller编译的时候出现No module named 'imp'