Function does not return value

2

Colleagues ...

I have a method that brings the results of a table. So far so good. What is puzzling me is that after I changed the server, the structure of this method is the same for two other results and works perfectly, but when creating another method with the same structure, it does not return value, except when there is only one value in the bank. I have already copied and pasted the structure of the functional method, replacing only the method name and nothing. But when I echo instead of return, the result appears. Therefore, I have 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. But when I give an 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?

    
asked by anonymous 11.05.2015 / 18:02

3 answers

3

To return all values concatenate a string using the .= operator, so that every iteration of for is the $c variable, it will receive the value already contained plus the current value. If you need to do some calculation or formatting on these values, it is best to store this 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());
    
11.05.2015 / 18:14
6

Note that return will only be called after for has run.

In other words, the for cycle will run in full, the value of $c will be 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 join a echo within the for cycle you are registering (doing echo) of each value that $i takes and that goes to $c , hence the difference. The end result is the same, the returned by return is the same. Note that in the version with the echo in the for loop you are sure to double the number 9 , the second the return of the function.

    
11.05.2015 / 18:07
4

In this function is returning only the last value of the vector. because it will have been assigned until it reaches the end of the vector, and when it arrives it 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, the value of $c is printed for each new cycle of for .

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

This is to assign to an array and printing all the content:

function teste(){
    $c = array();
    for($i = 0; $i < 10; $i++){
        $c[] = $i;
    }
    return $c;
}
var_dump(teste());
    
11.05.2015 / 18:08