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 4540478
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T15:05:30+00:00 2026-05-21T15:05:30+00:00

This is not just about Smarty, but I guess most template engines that have

  • 0

This is not just about Smarty, but I guess most template engines that have variables assigned. It’s more a theoretical question, than a practical. I have no use case.

What happens in PHP when you assign a big array $a to another variable $b? PHP copies the array? Maybe, just maybe, internally it creates a pointer. Then what happens when you alter $a slightly? $b shouldn’t be changed, because no & was used to create $b. Did PHP just double the memory usage??

More specifically: What happens when you assign a big array from you Controller ($a) to your template engine ($tpl->vars['a']) and to use in the view (extract to $a)? Did PHP’s memory just triple??

Now what happens if I assign all my variables by reference? I’m cool with my view being able to alter the array back into the Controller (I won’t be coming back there anyway). It’s also fine if the variable changes within the templat engine ($tpl->vars['a']).

Is assigning all vars by reference better for memory? Better for performance? If so: any chances of strange, unwanted side effects?

Because people like code and not stories:

// copies
$a = array( ... );
$tpl->assign('a', $a); // creates a copy (?) in $tpl->vars['a']

// pointer / by ref
$a = array( ... );
$tpl->assign_by_ref('a', $a); // creates a pointer in $tpl->vars['a'] because:

function assign_by_ref( $name, &$var ) {
  $this->vars[$name] = $var; // voila pointer?
}

I’m pretty sure PHP doesn’t mind big arrays and copies and clones, but performance and memory wise: which’s ‘better’?

edit
For objects, all of this doesn’t matter. Objects are always, automatically assigned by reference. And since objects are hot, maybe this is an outdated question, but I am very curious.

UPDATE
So PHP uses copy on write… Love it. And objects are always pointers. What happens when you:

$a = new BigObject;
$b = $a; // pointer, right?
$b->updateSomethingInternally(); // $b is now changed > what about $a?

Did this trigger the copy-on-write? Or are $a and $b still identical (like in ===)?

edit
Could I conclude that assigning by ref is really not worth it just to spare memory? PHP in itself is smart enough?

edit
Interesting visualization of copy, clone, by-ref etc: http://www.phpinsider.com/download/PHP5RefsExplained.pdf

  • 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-21T15:05:31+00:00Added an answer on May 21, 2026 at 3:05 pm

    PHP uses a concept called copy on write. I.e. if you just do a $a = $b PHP will not copy the whole value of $b to $a. It will just create some kind of a pointer. (To be more precise both $a and $b will point to the same zval and it’s refcount will be increased.)

    Now, if either $a or $b were modified the value obviously can’t be shared anymore and must be copied.

    So, unless you aren’t modifying the array in your template code, no copying will be done.

    Some further notes:

    • Beware of trying to optimize your code by blindly inserting references. Often they will have an effect contrary to what you expect. Example to explain why:

      $a = SOMETHING_BIG; // $a points to a zval with refcount 1 and is_ref 0
      $b = $a;            // $a and $b both point to a zval with refcount 2 and is_ref 0
      $c =& $a;           // Now we have a problem: $c can't just point to the same zval
                          // anymore, because that zval has is_ref to 0, but we need one
                          // with is_ref 1. So The zval gets copied. You now have $b
                          // pointing to one zval with refcount 1 and is_ref 0 and $a and
                          // $c pointing to another one with refcount 2 and is_ref 1
      

      So the contrary to what you wanted actually happened. Instead of saving memory you are actually allocating additional. It’s often hard to judge whether adding a reference will make it better or worse because it’s often hard to trace all different variables pointing to one zval (it’s often not as easy as it looks, just have a look at the examples of the debug_zval_dump function. So, really, the only safe way to know, whether a reference is good for performance or not, is to actually profile both variants.

    • Objects are, just like everything else, passed by value in PHP. Still you are right that they behave reference-like, because with objects this value that get’s passed around is just a pointer to some other data structure. In most cases the distinction between passing by reference and reference-like behavior isn’t of importance, but there still is a difference.

    This was just a short introduction to the topic. You can find a more in-depth analysis of the topic in a blog post by Sara Golemon with the porvoking title “You’re being lied to”.

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

Sidebar

Related Questions

This might not have a major usecase in projects, but I was just trying
This might be a dumb question but I'm just not sure about the answer.
This may sound like a stupid question but I'm a beginner not just to
This is not really a question because I just solved the problem, but I
I fear this just may not be possible, but I'm trying to create a
This is not a duplicate post, i just done research but not helping. First,
I'm just not too sure the proper way of doing this. I basically have
I know this is easy but i m just not able to debug it.
OBJECTIVE: To learn a little more about some technologies I'm familiar with but not
I just recently have started learning about the wonders of **kwargs but I've hit

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.