Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How bad of an idea is it to do this in PHP?

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading