0
<?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
  • echo $x which is the value you pass to the function. Commented Oct 27, 2016 at 16:11
  • "//this is the loop procedure to iterate 10 times" -- no, it iterates 11 times (0 to 10, including both of them) Commented Oct 27, 2016 at 16:14
  • You're echoing $i, which is the counter variable used in the loop. You want to echo the variable holding "xyz". echo $x; Commented Oct 27, 2016 at 16:15
  • Echo the '$x' variable instead of the '$i' which in this case is the for loop's index... Commented Oct 27, 2016 at 16:15

4 Answers 4

4

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

Sign up to request clarification or add additional context in comments.

Comments

0

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. 
?>

Comments

-1

If i well understand your question

for ($i = 0; $i <= 10; $i++){ 
        echo $y; //instead of xyz
    } 

Comments

-2
 <?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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.