Hello everyone I would like if you tell me how bad it would be to do something like this in php. Suppose I have a function in php that does a lot of things like calling functions and so on, but after calling the functions I no longer need the value they return, so would it be a bad idea to delete those variables as soon as I don’t need them anymore? Example:
class Example
{
public function something()
{
// do a bunch of things
// example
$isset = isset($_GET['some_value']);
if($isset)
{
unset($isset);
$some_result = some_function();
unset($some_result);
}
// do a bunch of things
}
}
What I am trying to show there is a function that after occupying the result of other functions, deletes those variables in order to make the program faster. That would be a good idea? would deleting the variables really make the program faster?
I hope you can help me, and if this is wrong I would appreciate if you tell me, since I am new to this and that is just an idea that came to my mind :D.
>Solution :
From my comment
PHP uses a garbage collector this means that when a variable is finished with, it will be automatically deleted when the program decides.
Manually doing this is unlikely to meaningfully change performance, and it makes code harder to maintain, so I would advise against it.
Additions
The linked docs say, in a small PHP program, the garbage collector will never run, as there are not enough unused variables to "bother" with clearing them up. All variables will unset when the whole program is finished.
This means that manually unsetting a variable may actually give worse performance, because you do an unnecessary operation, though the difference is very small.