Jump to content

Returning an array from a function.

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
3 replies to this topic

#1
ezcat

ezcat

    Learning Programmer

  • Members
  • PipPipPip
  • 39 posts
After a lot of searching I found how to return an array from a function. Many post were very confusing and complicated. The code below is simple and works just find, but what I don't understand is, why the "return" variable isn't the actual array variable. Here is the explanation that was with the code:

"The key is assigning the return value from the function to a new value ($newlist) and utilizing the indices in the new variable to do what you want."

Can any one explain this better than how it was explained above? Why isn't $list returned?



<?PHP

function arrFunc()

  {

    $list = array("one", "two", "three");

    return $list;

  }


$newlist=arrFunc();

print_r ($newlist);

// prints:  Array ( [0] => one [1] => two [2] => three ) 

?>



#2
Guest_Jordan_*

Guest_Jordan_*
  • Guests
The above function is assigning an array to a new variable. Variables in PHP are loosely typed meaning a variable can be of any type (array, integer, string, float, etc). In PHP (5+) the data in $list is transferred (cloned) into $newlist. The reason you use return statements in functions is because the scope of variables. Once arrFunc() completes the scope of $list ends and the garbage collector will terminate $list (this means you can't reference $list outside of arrFunc()).

Your above code is equal to:
$list = array("one", "two", "three");
$newlist=$list;
print_r ($newlist); 
You could also remove the return and pass by reference:
<?PHP
function arrFunc(&$list)
  {
    $list = array("one", "two", "three");
  }

$newlist=array();
arrFunc($newlist);
print_r ($newlist);
// prints:  Array ( [0] => one [1] => two [2] => three ) 
?> 
The & symbol in the function declaration parameter values (&$list) tells the function to pass by reference. This means that you are passing the address of the variable in memory to the function. $list and $newlist essentially point to the same block in memory.

#3
morefood2001

morefood2001

    Speaks fluent binary

  • Members
  • PipPipPipPipPipPipPipPip
  • 1,720 posts

ezcat said:

After a lot of searching I found how to return an array from a function. Many post were very confusing and complicated. The code below is simple and works just find, but what I don't understand is, why the "return" variable isn't the actual array variable. Here is the explanation that was with the code:

"The key is assigning the return value from the function to a new value ($newlist) and utilizing the indices in the new variable to do what you want."

Can any one explain this better than how it was explained above? Why isn't $list returned?


<?PHP
function arrFunc()
  {
    $list = array("one", "two", "three");
    return $list;
  }

$newlist=arrFunc();
print_r ($newlist);
// prints:  Array ( [0] => one [1] => two [2] => three ) 
?>

When you call a function, any return specified within that function makes the entire function act like a variable (not exactly, but this is how I think of it).

Just like you can do:
$var = "hi";
echo $var; //method 1
echo "hi"; //method 2, more direct
You can do

function hi(){
return "hi";
}
hi(); now will return a string "hi" when its called.

In this example, $var is also equivalent to hi();.

Hopefully with Jordan's explanation above and this, you will understand this better.

#4
ezcat

ezcat

    Learning Programmer

  • Members
  • PipPipPip
  • 39 posts
Ah, now I understand what is going on.

Thanks,
Jon