Directly manipulate $_GET and $_POST variables

Asked

Viewed 255 times

1

I have an application that will work with a large amount of data to be manipulated and stored in a database, so it is indicated to store the values present in variables $_GET and $_POST in intermediate variables (which would result in spent memory duplication) or directly change $_GET and $_POST?

  • The memory or processing supposedly wasted is tiny for you to worry about. If I remember correctly, what you should avoid is using those "famous" ternaries when the variable they tested contains enough information. Other than that, focus on avoiding other gluten traps throughout the Application and you will be covered.

  • Okay, in another scenario, where there are "millions" of bank submissions, one could directly manipulate the superglobal in order to save memory?

1 answer

4


There is nothing to stop you from manipulating this variable. I don’t know anything that indicates that it will create a problem. There is nothing in the documentation that says you should avoid. But some weightings should be noted.

  • PHP is not the best language to work with large volumes of data at once. Much less is the language to think about when expecting great optimization of resource use. If you really need this, choose the right tool.
  • With $_GET you cannot manipulate millions of data. Never even thousands will come if the protocol is respected.
  • If you will receive millions of fields in a single request you are doing something very wrong, there are so many other problems that can occur, so much to worry that this problem comes to be almost irrelevant.
  • PHP does not keep data in memory that is no longer referenced. At least it says it does not. So I don’t know if I’d have this duplicate that you expect.
  • You need to see if you really need to duplicate. I understand that you want to write in this array. But almost always what people want is just to read its content and use it somewhere. There’s no problem in this. Nothing. I notice that some programmers think it is necessary to play a field coming through $_POST in a local variable and then use the variable. This is really a waste probably caused by people’s insistence on following cake recipe instead of learning to develop software.
  • Other than that, it’s completely unnecessary optimization.
  • If you still insist on this, do a test stress and come to some conclusion for your real case. No one can give you better information than this.

The exception would be if using threads, which I doubt is the case. By the nature of PHP programs they rarely benefit a program. Again, if someone thinks they need to thread to get better performance she will probably choose another language.

  • ,@bigown, in: With $_GET you can not manipulate millions of data, I expressed myself badly, would be thousands of people sending data (personal registration), to a server and not a single person sending thousands of data

  • The executions are ephemeral and the general structure to manipulate the script takes up so much space that it is unlikely that this use will make any fundamental difference. If you do, PHP is not the language for you to use.

  • Depends. If the script has a complex logic which has not been optimized correctly where possible, the execution will be slow and the competition (which is what worries the OP) can kill the Application. Unfortunately the superglobals are not read-only, then you can do whatever you want with them. But it’s a matter of common sense.

  • @Brunoaugusto "not optimized correctly" this is the point, being reused variable is not correct optimization. Neither I nor the AP speak in competition at any time. But if competition is the problem, the solution is also another.

  • 2

    Then you need to better understand what competition is.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.