Function does not return value

Asked

Viewed 852 times

2

Colleagues...

I have a method that brings the results of a table. So far so good. What is intriguing me is that after I changed server, the structure of this method is the same for two other results and they work perfectly, but when creating another method with the same structure, it does not return value, except when there is only one value in the database. I have copied and pasted the structure of the functional method, replacing only the name of the method and nothing. But when I echo instead of Return, the result appears. Therefore, I isolated the code in another file and in a simple example, the error appears. Look at a simple function:

function teste(){
        for($i = 0; $i < 10; $i++){
            $c = $i;
        } 
  return $c;  
} 
echo teste();

The result appears only 9. However when I echo. Returns the values correctly:

function teste(){
    for($i = 0; $i < 10; $i++){
        $c = $i;
        echo $c;
    }
}
echo teste();

Does anyone know why this happens?

  • Some of the answers was useful to you? if not post what did not work post what is still happening if yes validate a reply.

3 answers

6

Takes into account that the return will only be called after the for have run.

That is, the cycle for will run in full, the value of $c is being rewritten until the cycle ends. Then, when the cycle ends, the last value that $c has is 9 and it is this value that will be passed to return.

When you put a echo within the cycle for you are recording (echo) of each value that the $i take and that passes to $c, hence the difference. The final result is the same, returned by return is the same. Notice that in the version with the echo within the cycle for You’re sure twice the number 9, the second o return of function.

  • 2

    Thank you Sergio

4

This function returns only the last vector value. for is will be assigning until the end of the vector, and when it arrives will return the last value assigned to $c

function teste(){
        for($i = 0; $i < 10; $i++){
            $c = $i;
        } 
  return $c;  
} 
echo teste();

In this other one is printing the value of $c each new cycle of for.

function teste(){
    for($i = 0; $i < 10; $i++){
        $c = $i;
        echo $c;
    }
}
echo teste();

This one assigns to an array and prints all content:

function teste(){
    $c = array();
    for($i = 0; $i < 10; $i++){
        $c[] = $i;
    }
    return $c;
}
var_dump(teste());
  • 2

    Obrigado Ricardo.

3


To return all values concatenate a string using the operator .=, thus for each iteration of the is the variable $c will receive the value already contained plus the current one. If you need to do some calculation or formatting in these values it is better to keep it in an array.

Version with string:

function teste(){
   $c = '';
   for($i=0;$i<10;$i++){
      $c .= $i .' ';
   }
   return $c;
}

Version with array:

<?php
    function teste(){
       $c = array();
       for($i=0;$i<10;$i++){
          $c[] = $i; 
       }
       return $c;
    }

    echo '<pre>';
    print_r(teste());

Browser other questions tagged

You are not signed in. Login or sign up in order to post.