0

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.

DFG
  • 31
  • 1
  • 13
  • 1
    PHP uses a [garbage collector](https://www.php.net/manual/en/features.gc.performance-considerations.php) 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. – Jess Sep 03 '22 at 22:28
  • To paraphrase Jess; those two variables are going to be unset when they go out of scope, whether or not you call unset. But, just because something is unset, doesn't mean that the memory is freed up straight away, that only happens when the garbage collector cleans it up. So, this is deeply redundant, won't improve performance, and actively makes the code harder to read and maintain. – MatBailie Sep 03 '22 at 22:33
  • It would only make sense if the variable holds a huge amount of data and you need to conserve memory. – IT goldman Sep 03 '22 at 22:53
  • Also note that your example can be refactored to eliminate that variable entirely, like `if (isset($_GET['some_value'])) { ... }`. I'm sure it's just a simple example, but still. :-) – Greg Schmidt Sep 04 '22 at 04:34

1 Answers1

2

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.

Jess
  • 177
  • 5
  • A note for all further readers of this answer: it is entirely wrong. It confuses a garbage collector with unsetting a variable. And therefore makes a completely wrong conclusion – Your Common Sense Sep 04 '22 at 05:05
  • Could you explain why? I'd be happy to further clarify – Jess Sep 06 '22 at 21:12