1

I am new to the world of coding as well as PHP and am wondering how I can use return when looping. For example I would like to return/display 1-10 however not use echo.

$start = 1;
$end = 11;

for($start; $start < $end; $start=$start+1) {

echo $start; //how can I use return?

}
3
  • Why do you want to return something? And to where? Commented May 4, 2011 at 23:16
  • 1
    More interesting: Where do you want to return something from? I dont see any function. And if you want to display something, whats the matter with echo? Commented May 4, 2011 at 23:17
  • @Dogbert/KingCrunch - The reason I would like to return something is because I wish to return the results into another function Commented May 4, 2011 at 23:46

6 Answers 6

10

Well, return will exit the function, so if you put return in a loop, the loop will only do one iteration (until the return statement).

You can collect all the values in an array and return the array:

function myFunction() {
    $start = 1;
    $end = 11;
    $values = array();

    for($start; $start < $end; $start=$start+1) {
       $values[] = $start;
    }

    return $values;
}

That said, a function generating consecutive numbers already exists: range().

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

1 Comment

Thanks. The reason I ask is if I wish to return the values of one function into another. Veryhelpful answer. Didn't think we could feed the values into an array.
2

return is for sending the results of a function call back to the function or script that called it. It's the opposite of passing parameters to a function.

What you're doing is looping over a variable in the same scope, so return is not needed. Printing is done via echo or print. However, you may choose to build a value in that loop and print that once the loop is completed.

Additionally, if you're in a loop and you want to stop that loop immediately, use break; and if you want to skip the iteration you're on and go to the next one, use continue.

Here's some additional reading.


More clarification on continue. Say, for whatever reason, we don't want to do anything when $i is 6:

$start = 1;
$end = 11;

for ($i = $start; $i < $end; $i++) 
// changed this to iterate over $i for readability/clarity
{
    if ($start == 6)
    {
        // essentially, continue just skips this iteration of 
        // the loop, goes back to the top, iterates $i based on 
        // that third parameter in the for() declaration, and 
        // continues on.

        continue;
    }


    echo $start; //how can I use return?
}

// output: 1234578910

1 Comment

Thanks. Did not know about continue. How would I use that in the example of my code?
1

just use

function foobar()
{   
   $output = '';
   for ( $i = 1 ; $i <= 10 ; $i++ )
   {
      $output .= $i;
   }
   return $output
}

echo foobar();

1 Comment

Thanks. Very simple solution. I like it. Very helpful.
1

You can achieve it by defining a variable you want to return inside and outside the loop with .=.

Here is a working example:

function my_function() {

// I am the variable to be return-ed
$k = "My World<br/>";

for ($z=1; $z<=3; $z++) {
$v = 'Here'.$z;

// I am the same variable but I am going inside the loop
$k .= $v . "<br/>";
}

//I am not always important to be here!
$k .= "Is Awesome";

// This is the show time..
return $k;

} 

my_function();

Output:

My World
Here1
Here2
Here3
Is Awesome 

Comments

0

Return will end the function that you are currently in. The scenario you have given us is not a good scenario to use the return function.

The example below will do the same as your code, but uses a function as well as your for loop. Hope this answeres your question.

$start = 11;
$end = 20;

for($start; $start <= $end; $start++){
     echo calculateValue($start);
}

function calculateValue($value){
    return $value - 10;
} 

1 Comment

Thanks. Sorry for not giving a very good scenario. I am wanting to learn how to return the values of a function into another.
0
 function loop_kec($ar_kec) {
        $total_data = count($ar_kec) - 1;
        //$return_kec = array();
        for ($e = 0; $e < $total_data; $e++) {
            $return_kec .= 'kecamatan = ' . "'$ar_kec[$e]'" . ' or ';
        }
        $return_kec .= 'kecamatan = ' . "'$ar_kec[$total_data]'";
        return $return_kec;
    }

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.