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

  • SEARCH
  • Home
  • 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 8007739
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T17:55:43+00:00 2026-06-04T17:55:43+00:00

In the PHP documentation it says: Do not use return-by-reference to increase performance. The

  • 0

In the PHP documentation it says:

Do not use return-by-reference to increase performance. The engine
will automatically optimize this on its own.

I wish to return a reference to an array (which is a property of my class). How does PHP optimize this, because the array is not an object?

If the array has 1 billion entries, won’t I get two arrays with 1 billion entries stored in memory if I don’t pass it by reference?

  • 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-06-04T17:55:44+00:00Added an answer on June 4, 2026 at 5:55 pm

    PHP uses copy on write. That means that if you pass the huge array as a function parameter and only read from it (such as a foreach), you won’t be writing to it so it doesn’t need to make a copy.

    $count = count($huge_array); // read the reference at the bottom
    for($i = 0; $i < $count $i++) {
        $value = 2*$huge_array[$i]/15;
        if($value > 3)
            $sub_array []= $value;
    }
    

    The $subarray which should be smaller (it is a subset of the huge array), will contain only the needed (changed) data.

    If you do not intend on changing the values of the original $huge_array it will never get copied so no extra memory is used.

    If you intend on changing the original values of the array, you need to pass it by reference.

    If you intend on making and returning an altered version of the original array then you do need the extra memory PHP is allocating for you.

    If you intend on making and returning an altered smaller subset of the original array then you will create a new empty array into which you will copy some of data from the huge array and should be careful not to overwrite values in the $huge_array, so you’ll avoid writing from $huge_array and emphasise on reading from it.

    This link explains that PHP was optimised for pass by value use cases.

    Copy on write only works if the variable isn’t a reference being passed to a function expecting a value, if it is, passing it around triggers a copy.

    That makes PHP native functions that expected an argument to be passed by value and received a referenced variable copy the value of the reference.

    function foo(&$data) {
        for ($i = 0; $i < strlen($data); $i++) {
            do_something($data{$i});
        }
    }
    
    $string = "... looooong string with lots of data .....";
    foo(string);
    

    Executing strlen in C would imply iterating over the entire string to count it. In PHP strings have an attached length value. So strlen returns the value read from there (fast).

    But if you give it a referenced variable it will have to copy it before reading it’s length so it will iterate over the value to copy it into it’s argument list, read and return the length (and subsequently release the memory for the freshly copied string).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The PHP documentation says the following about the __call() magic method: __call() is triggered
if (false == 0) echo true\n; // => true While PHP documentation says: var_dump((bool)
Constructor for PHP's exception has third parameter, documentation says: $previous: The previous exception used
Here is the PHP documentation Here is how I would use it in an
The documentation here is not very clear: http://www.trirand.com/jqgridwiki/doku.php?id=wiki:inline_editing&s[]=editurl#saverow Do I have to manually make
Oh well, maybe it's my ignorance, but PHP documentation about include statement says take
I am wondering if its possible to have Netbeans display PHP Documentation for functions/methods/classes
The PHP documentation for the mongo class says using a cursor instead of iterator_to_array
Ok, this might be a very noob question, but I find that PHP Documentation
Hi found below forum which describes not to use Spring hibernate template: http://forum.springsource.org/showthread.php?111771-what-is-the-difference-between-hibernate-template-and-hibernate-dao-support .

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.