6

I happens to read this http://code.google.com/speed/articles/optimizing-php.html

It claims that this code

$description = strip_tags($_POST['description']); echo $description; 

should be optimized as below

echo strip_tags($_POST['description']); 

However, in my understanding, assignment operation in PHP is not necessarily create a copy in memory.

This only have one copy of "abc" in memory.

$a = $b = "abc"; 

It consumes more memory only when one variable is changed.

$a = $b = "abc"; $a = "xyz"; 

Is that correct?

1
  • In your first code chunk, can't you reference $description later on? Doesn't that imply that the string is being stored in memory? The second chunk, though, can't be referenced again. You'd have to re-evaluate the function once more to get the same output. Commented Jun 4, 2011 at 3:43

3 Answers 3

5

should be optimized as below

It's only a good idea if you don't need to store it, thereby avoiding unnecessary memory consumption. However, if you need to output the same thing again later, it's better to store it in a variable to avoid a another function call.

Is that correct?

Yes. It's called copy-on-write.

Sign up to request clarification or add additional context in comments.

3 Comments

Does it really have "unnecessary memory consumption"? I mean, if $_POST['description'] is 512K, does the first block code consumes double memory? I suppose it takes same memory, just the memory would not be released before script ends.
@Morgan Cheng: $_POST['description'] and $description are different, because $description is a copy of $_POST['description'] to which you applied a modification (strip_tags). So yes, it takes more memory than just referencing the POST variable. Also, memory is not necessarily freed at the script end, it is freed when the variable goes out of scope.
@netcoder The memory is doubled anyway, since the modified data needs to be stored somewhere, however brief. The only difference is that directly echoing the function return value allows the memory to be reclaimed immediately, since no reference to it exists anymore.
1

In the first example, if the variable is only used once then there is not point of making a variable in the first place, just echo the statements result right away, there is no need for the variable.

In the second example, PHP has something called copy on write. That means that if you have two variables that point to the same thing, they are both just pointing at the same bit of memory. That is until one of the variables is written to, then a copy is made, and the change is made to that copy.

Comments

1

The author does have a point insofar as copying data into a variable will keep that data in memory until the variable is unset. If you do not need the data again later, it's indeed wasted memory.

Otherwise there's no difference at all in peak memory consumption between the two methods, so his reasoning ("copying") is wrong.

2 Comments

Well, variables will be unset automatically when they go out of scope. What you mention is true in procedural programming, but not really a concern in a properly structured OO application.
@netcoder Granted, sure. But if you're going to generate a lot more data in the same scope it can make the difference between a running script and a terminating script. :)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.