Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7172621
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T15:39:09+00:00 2026-05-28T15:39:09+00:00

Possible Duplicate: In PHP (>= 5.0), is passing by reference faster? Say I’m dealing

  • 0

Possible Duplicate:
In PHP (>= 5.0), is passing by reference faster?

Say I’m dealing with a large-ish (just how big isn’t important) array of data and passing it into a function. It will, of course, be passed by value and not by reference by default. Okay, I know that.

So my question is:

If the data stored in this array were large enough, can worthwhile
performance or memory usage gains be experienced by storing the data
in an object (say, a stdClass) or by using foo(&$myData) so that the data is
passed by reference instead of value?

My understanding is that when it’s passed by value PHP will create a whole new pointer to a whole new memory allocation for the data while the passed-by-value variable exists in the function scope. In such an event, might passing a large amount of data by reference be worthwhile for this reason? I don’t know if this understanding is correct … that’s part of the question.

Note: this is a hypothetical, so don’t bring up anything about “premature optimization”, “you’d be better off to minimize disk I/O before worrying about this”, “depends on how much RAM you have”, “etc.”

UPDATE

A further question: say the data is being passed to an object constructor to be stored as one of that object’s properties and it’s being modified in some small way as it’s stored in the new object’s property. Will there then be a significant difference in the memory usage aspect of the operation? Is it correct to assume that it will be better to define the data in the destination format to begin with to avoid the cost incurred by altering the data down the line?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T15:39:10+00:00Added an answer on May 28, 2026 at 3:39 pm

    I cannot find anything official, but from my understanding the Zend Engine will only copy the array when you attempt to write to it. That is, there is no copy made in this following code:

    function printFirst($array) { echo $array[0]; }
    
    $array = range(1,1000);
    printFirst($array);
    

    However, in this code, a copy will be made, assuming no other optimizations are made (ie: this example function doesn’t do anything, so it may be excluded entirely).

    function changeFirst($array) { $array[0] = 101; }
    
    $array = range(1,1000);
    changeFirst($array);
    

    So to answer your question, no, passing by reference will not make any difference. If you actually need to modify the original, then of course you need a reference either way.

    Note, I found a duplicate question here: In PHP (>= 5.0), is passing by reference faster?
    And here: How does PHP assign and free memory for variables?

    Update

    This is the code I ran which demonstrated a significant memory usage by the object, though not as much as the array itself (about 2/3).

    <?php
    
    class A {
    
    public $data;
    
    public function __construct($data) {
            $this->data = $data;
            // slight change
            $this->data[0] += 1;
    }
    
    };
    
    $baseUsage = memory_get_usage();
    $array = range(1,1000);
    $arrayUsage = memory_get_usage();
    echo "Array took: ", $arrayUsage - $baseUsage, "\n";
    $a = new A($array);
    echo "Object took: ", memory_get_usage() - $arrayUsage;
    
    ?>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: In PHP (>= 5.0), is passing by reference faster? I know the
Possible Duplicate: In PHP (>= 5.0), is passing by reference faster? I wonder if
Possible Duplicate: Reference: Comparing PHP's print and echo Is there any major and fundamental
Possible Duplicate: php == vs === operator Reference - What does this symbol mean
Possible Duplicate: PHP passing parameters via URL I know that if you have a
Possible Duplicate: Passing javascript variable to PHP Hi I wonder if it's possible to
Possible Duplicate: PHP: Notice: Undefined variable and Notice: Undefined index I am just learning
Possible Duplicate: Passing JavaScript Array To PHP Through JQuery $.ajax I'm trying to pass
Possible Duplicate: What is the safest way of passing arguments from server-side PHP to
Possible Duplicate: PHP detecting request type (GET, POST, PUT or DELETE) This should be

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.