0

This is not an bug or issue. But while I was reading about a lot of performance realated articles I came to know about how javascript variables take more resources to load and so it is better to put the global variable for javascript into a local variable to work - specially in for loops.

So I was wondering if such thing also happens on $_POST with PHP. As in will it give me a performance improvement if I have lot of post data. Save that in a local array say $post_data = $_POST;

And then I reference in where required.

Amitav Roy
  • 741
  • 1
  • 11
  • 21

4 Answers4

2

Not really worth the time. $_POST is already an array in memory, so no need to duplicate it.

To get the size of $_POST in bytes, you can do this:

strlen(serialize($_POST));

(serialize will turn any object into a string which can be saved and unserialized. )


FWIW in for loops, if you don't cache the length of the array and instead use something like this...

for(var i = 0; i < myarray.length; i++) {}

...JS will have to recalculate .length each time it starts an iteration, so it's expensive, particularly over big arrays.

Ben
  • 54,723
  • 49
  • 178
  • 224
0

In php the main concern with globals is memory. Performance difference in accessing a global or local scoped variable is negligable.

nvanesch
  • 2,540
  • 14
  • 25
0

Not sure about the JavaScript part (can you provide a reference, please?). In PHP this would effectively create a copy of $_POST array and hence would use more memory without having any effect on speed improvement.

naivists
  • 32,681
  • 5
  • 61
  • 85
0

If you make:

$post_data = $_POST;

PHP will create just reference to info in $_POST variable, so you will not have more memory usage, till changind info in $post_data. If you try to change data when PHP will create copy of information in memory.

Narek
  • 3,813
  • 4
  • 42
  • 58