Arrays as Function Argument

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • nunoperalta
    New Member
    • Jul 2007
    • 19

    Arrays as Function Argument

    Hi, everyone! :)

    I would like to know if an array, when given as function argument, is cloned (occupying more memory), or it is just referenced (but not modified as global variable).

    I will explain:
    $array = array(2, 3);
    $var = $array; // this is reference, if I change $var, I will change $array

    -------

    And I am asking about this:

    function checkArray($ar) {
    return ($ar[1] > 0);
    }

    $array = array(1, 2, 3);
    $var = checkArray($arr ay);

    In this case, will $array be recreated/cloned to checkArray()?
    I just want to know, because I have a function that may *SOMETIMES* need to check two arrays, and I have always to give those arrays as argument, but I don't want to waste memory when those arrays are not needed...

    Thanks in advance for your help!
  • Dormilich
    Recognized Expert Expert
    • Aug 2008
    • 8694

    #2
    Originally posted by nunoperalta
    $array = array(2, 3);
    $var = $array; // this is reference, if I change $var, I will change $array
    I doubt that, references are usually made with the & operator
    Code:
    $var = &$array;

    Comment

    • nunoperalta
      New Member
      • Jul 2007
      • 19

      #3
      Originally posted by Dormilich
      I doubt that, references are usually made with the & operator
      Code:
      $var = &$array;
      this is deprecated in PHP6. I am sure about what I said in that part. You can test yourself :) Thanks, anyway.

      Comment

      • Dormilich
        Recognized Expert Expert
        • Aug 2008
        • 8694

        #4
        Originally posted by nunoperalta
        this is deprecated in PHP6. I am sure about what I said in that part. You can test yourself :) Thanks, anyway.
        I tested i (though PHP 5.2.10) and yours didn't work.

        and I'd like to know where this (about deprecation) is stated for data types other than objects (where this indeed is deprecated).

        Comment

        • nunoperalta
          New Member
          • Jul 2007
          • 19

          #5
          Originally posted by Dormilich
          I tested i (though PHP 5.2.10) and yours didn't work.
          wow... how come I tested this long ago and it worked before!!!

          It failed, really!

          That's ok then, I will use reference operator in the function arguments, then, so the arrays will not be cloned, right? Will it work even if the given arguments are null?

          Thanks!

          Originally posted by Dormilich
          and I'd like to know where this (about deprecation) is stated for data types other than objects (where this indeed is deprecated).
          well, I read about $var = &new Object(); , and I thought it was for all references in this way... $var = & *something*;
          Sorry :)

          Comment

          • nunoperalta
            New Member
            • Jul 2007
            • 19

            #6
            Just tested again.
            Only Objects are referenced automatically. Maybe I tested with an object, and thought it would be the same for Arrays.

            I thought that, because other programming languages, like Python, do that.
            Thanks again!

            Comment

            Working...