Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 711245
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T04:42:48+00:00 2026-05-14T04:42:48+00:00

I am somewhat new to PHP, and I am wondering: How important is it

  • 0

I am somewhat new to PHP, and I am wondering:

How important is it to unset variables in PHP?

I know in languages like C, we free the allocated memory to prevent leaks, etc. By using unset on variables when I am done with them, will this significantly increase performance of my applications?

Also, is there a benchmark anywhere that compares the difference between using unset and not using unset?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T04:42:49+00:00Added an answer on May 14, 2026 at 4:42 am

    See this example (and the article I linked below the question):

    $x = str_repeat('x', 80000);
    echo memory_get_usage() . "<br>\n";      // 120172
    echo memory_get_peak_usage() . "<br>\n"; // 121248
    
    $x = str_repeat('x', 80000);
    echo memory_get_usage() . "<br>\n";      // 120172
    echo memory_get_peak_usage() . "<br>\n"; // 201284
    

    As you can see, at one point PHP had used up almost double the memory. This is because before assigning the ‘x’-string to $x, PHP builds the new string in memory, while holding the previous variable in memory, too. This could have been prevented with unsetting $x.

    Another example:

    for ($i=0; $i<3; $i++) {
        $str = str_repeat("Hello", 10000);
        echo memory_get_peak_usage(), PHP_EOL;
    }
    

    This will output something like

    375696
    425824
    425824
    

    At the first iteration $str is still empty before assignment. On the second iteration $str will hold the generated string though. When str_repeat is then called for the second time, it will not immediately overwrite $str, but first create the string that is to be assigned in memory. So you end up with $str and the value it should be assigned. Double memory. If you unset $str, this will not happen:

    for($i=0;$i<3;$i++) {
        $str = str_repeat("Hello", 10000);
        echo memory_get_peak_usage(), PHP_EOL;
        unset($str);
    }
    
    // outputs something like
    375904
    376016
    376016
    

    Does it matter? Well, the linked article sums it quite good with

    This isn’t critical, except when it is.

    It doesn’t hurt to unset your variables when you no longer need them. Maybe you are on a shared host and want to do some iterating over large datasets. If unsetting would prevent PHP from ending with Allowed memory size of XXXX bytes exhausted, then it’s worth the tiny effort.

    What should also be taken into account is, that even if the request lifetime is just a second, doubling the memory usage effectively halves the maximum amount of simultaneous requests that can be served. If you are nowhere close to the server’s limit anyway, then who cares, but if you are, then a simple unset could save you the money for more RAM or an additional server.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am still somewhat new to php and was wondering the best way to
I'm somewhat new to OpenGL. I've used it for extremely simple stuff like rendering
I'm new to the MVC concept, and somewhat new to PHP. Question 1 Before
I'm somewhat new to Javascript, not HTML/CSS or PHP/MySQL, just JS. I have a
I am new to PHP and I am somewhat understanding it. Now I am
I am somewhat new to qt/c++ (coming from a php/sql world) and I am
Somewhat new to php, I did some searching but didn't find a clear answer.
So I am still somewhat new to PHP, MySQL, and Javascript but I have
I am somewhat new to PHP coding and I am aware that malicious users
I am somewhat new to PHP and MySQL, and have just setup a basic

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.