Technically and practically, in both forms, no problem.
What can define whether a technique, style or even a concept is more appropriate than another is the ultimate goal.
The data will be used for what purpose?
From this point it is decided which technique to use.
The technique using an array to receive the array $_GET
or $POST
has the advantage that it can be simpler to manage. As an example, if you need to delete everything, just delete a single object.
$f['titulo'] = $_POST['titulo'];
$f['conteudo'] = $_POST['conteudo'];
$f['data'] = $_POST['data'];
/**
Aqui faz os paranauê..
*/
/**
Após terminar tudo que tinha para fazer, podemos descartar simplesmente assim:
*/
unset($f);
/**
No outro modo teria que fazer isso:
*/
unset($titulo);
unset($conteudo);
unset($data);
/**
Uma forma resumida
*/
unset($titulo, $conteudo, $data);
*That is a mere example. Obviously if that were the only advantage, it would be ridiculous.
After all, which the best?
There is, in this case, the best. Whenever you think "which is best," always think that there are N ways to solve something by achieving the same result. Instead of thinking "what’s best," think "what’s best for this business model?".
For any technique, think about where you want to go. What is the goal. What is the result you expect to get.
Also think about the performance. What costs more to be executed?
Is it worth using a more sophisticated resource that consumes more processes for something small and simple? It’s worth choosing a simple path because it’s easier even if you have to modify it in the future for an implementation or create something robust and modular to simplify future implementations?
Choices depend on planning, which is based on the business model.
Let’s see in practice, a more sophisticated situation.
Why not use global variables directly $_POST
and $_GET
? Why consume memory by "cloning" the same data?
In more sophisticated systems, we may want to keep the original data received by $_GET
and $_POST
, because at the time of receipt, we need to make validations, sanitizations and filtrations.
A sanitization or filtration can modify the original data received. For a business model that wants to see the original data, it is not good business. Therefore, in that case it is more appropriate to "clone" the data to a new object where the data will be manipulated and, the original data will remain intact.
Eliminate redundancies
A simple tip that avoids code repetition.
Instead of getting one by one
$f['foo'] = $_POST['foo'];
$f['bar'] = $_POST['bar'];
Just take the entire array
$f = $_POST;
If you want to create variables with index names, use variable variables in a loop
foreach ($_POST as $k => $v)
$$k = $v;
You can’t say anything in this context. Assigning a key array to another is pretty cool...
– rray
@rray did not understand his phrase. Why is cool?
– Marcos Vinicius
Why doesn’t it make sense for you to copy an item-by-item array, for what purpose? why not use $_POST straight? what else is done with
$f
?– rray
Where did you get that reference ?
– Edilson
If you are creating new variables just to store the data of another variable, in most cases it would just be memory waste. Use the original variable.
– Kazzkiq
For God’s sake! Don’t give answers teaching to use
extract
directly in$_POST
, otherwise you will create a programmer who creates unsafe code !– Wallace Maxters
Today, even learning is a risk. There are still many other practices that are not recommended in the answers.
– Edilson
@Any Marcosvinicius helped you more? You need something to be improved?
– Maniero