Technically and in terms of both ways, there is no problem.
What can define whether a technique, style or even a concept is more appropriate than another is the ultimate goal.
Will the data be used for what purpose?
From this point you decide 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);
* This is a simple example. Obviously if that was the only advantage, it would be ridiculous.
After all, what's the best?
There is no better way to do this. Whenever you think "which is the best," always think that there are N ways to solve something by reaching the same result. Instead of thinking "which is best", think "which one is right for this business model?".
For any technique, think about where you want to go. What's the point. What outcome do you expect to achieve?
Think of performance as well. What costs more to run?
Is it worth using a more sophisticated feature that consumes more processes for something small and simple? Is it worth choosing a simple path because it is 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 the global variables $_POST
and $_GET
directly? Why consume memory by "cloning" the same data?
In more sophisticated systems, we may want to keep the original data received for $_GET
and $_POST
, because at the moment of receipt, we need to validate, sanitize, and filter.
A sanitization or filtering can modify the original data received. For a business model that wants to see the original data, it is not a good deal. So in this 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.
Delete redundancies
A simple tip that prevents code repetition.
Instead of receiving one by one
$f['foo'] = $_POST['foo'];
$f['bar'] = $_POST['bar'];
Only receive the entire array
$f = $_POST;
If you want to create variables with index names, use variable variables in a loop of repetition
foreach ($_POST as $k => $v)
$$k = $v;