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

如何在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.

图片转代码服务由CSDN问答提供 功能建议

 &lt;?php 
 function iterate($ x){//这是函数
  for($ i = 0; $ i&lt; = 10; $ i ++){//这是循环过程迭代10次
 echo $ i;  //它将在屏幕上显示0123456789 
} 
} 
 
 $ y =“xyz”;  //变量声明
 echo iterate($ y);  //应该迭代xyz 10次。  
?&gt; 
   
 
 

希望在php函数内使用for循环回显(打印)xyz十次,结果不符合预期。 显示 xyz迭代十次。

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

4条回答 默认 最新

  • dongpu3347
    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

    点赞 评论
  • dra11767
    dra11767 2016-10-27 16:16
     <?php
    
         function iterate($x){ //this is the function
             for ($i = 0; $i < 10; $i++){ //this is the loop procedure to iterate 10 times
                 echo "{$x}<br/>"
             } 
         }
    
         $y = "xyz"; //variable declaration
         echo iterate($y); //should be iterate xyz as much 10 times.
    
    点赞 评论
  • douhao3562
    douhao3562 2016-10-27 16:20

    If i well understand your question

    for ($i = 0; $i <= 10; $i++){ 
            echo $y; //instead of xyz
        } 
    
    点赞 评论
  • dongzhao5970
    dongzhao5970 2016-10-27 16:22

    looks like you are confused on what you are doing.

    You are printing the $i which is used for iteration not the one you passed (i,e. $x)

    to fix this, you should echo $x which is the one you want to print

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

    now that the logic is fixed, there still some problem here you are printing the function whose role is to print the xyz.

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

    combining both solution:

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

相关推荐