SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Wizard
    Join Date
    Apr 2002
    Posts
    2,307
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)

    passing array to function: values or pointer passed?

    when you pass an array to a function via the function's parameters what is passed? surely it can't be the values of the array but a pointer? (i'm saying that with C language in mind). but if you modify the array and end the function without returning the array, changes to the array are not apparent and it seems returning the array is necessary, which then makes my initial assumption that the a pointer is passed look incorrect.

    in the following the array never progresses. i thought it was going to. what's the best way to do the below so it does progress?

    Code:
    <?php
    
    $array = array(1);
    
    function func($array) {
    	end($array);
    	$index = key($array);
    	$array[$index+1] = $array[$index]+1;
    }
    
    for($i=0; $i<10; $i++)
    	func($array);
    
    echo '<pre>';
    print_r( $array );
    echo '</pre>';
    
    ?>
    thanks

  2. #2
    SitePoint Wizard
    Join Date
    Apr 2002
    Posts
    2,307
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    ah, putting an & infront of the array in the call has done it:

    func(&$array);

    so without the & (which makes it pass a pointer obviously, just like C) are the values of an array passed? must be. surprising. that's not like C i don't think, although it's easy to get confused on this sort of thing.

  3. #3
    SitePoint Guru
    Join Date
    Jul 2005
    Location
    Orlando
    Posts
    634
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Arrays are always passed by value. As you found out, putting the & in front of any variable passes it by reference.

    In PHP5, objects are automatically passed by reference. Arrays are still not. In PHP4 objects are passed by value (boo PHP4).

  4. #4
    SitePoint Wizard
    Join Date
    Apr 2002
    Posts
    2,307
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    i see, thanks. seems like that might be pitfall if you didn't know about it from an efficiency point of view: if you have a very large array i guess passing it to a function by value is a very inefficient thing to do. something to remember regardless of whether changes to the array made in the fucntion are required to stick after the function ends.

  5. #5
    . shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by johnyboy View Post
    i see, thanks. seems like that might be pitfall if you didn't know about it from an efficiency point of view: if you have a very large array i guess passing it to a function by value is a very inefficient thing to do. something to remember regardless of whether changes to the array made in the fucntion are required to stick after the function ends.
    Actually it doesn't matter in the slightest. Internally PHP doesn't copy anything it only stores the changes and spits out the result. So I've been told by one of our PHP gurus.
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.


  6. #6
    SitePoint Wizard
    Join Date
    Apr 2002
    Posts
    2,307
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    i see, thanks.

    and just for the record i said "putting an & infront of the array in the call has done it". the & doesn't need to go there but in front of the array variable in the function definitition line:

    function func(&$array) {
    ...
    ...


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •