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 8438405
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T07:42:22+00:00 2026-06-10T07:42:22+00:00

First, I apologize if this just a coding style issue. I’m wondering about the

  • 0

First, I apologize if this just a coding style issue. I’m wondering about the pros and cons of assign a new variable for each property or function to just to re-assign an existing variable. This is assuming you don’t need access to the variable beyond the scope.

Here’s what I mean (noting that the names $var0,... are just for simplicity),
Option#1:

$var0= array('hello', 'world');
$var1="hello world";
$var2=//some crazy large database query result
$var3=//some complicated function()

vs.
Option#2:

$var0= array('hello', 'world');
$var0="hello world";
$var0=//some crazy large database query result
$var0=//some complicated function()
  1. Does it depend on the memory size of the existing variable? I.e., is re-assigning memory more computationally expensive that assigning a new variable?
  2. Is this always a scope issue, meaning you should use Option#2 only if you don’t need each of the variable values outside the scope shown here?
  3. Does it depend on what each variable value is? Does re-assigning to different data types have different costs associated with it?
  • 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-06-10T07:42:24+00:00Added an answer on June 10, 2026 at 7:42 am

    Personally I would say you should never ever reassign a variable to contain different stuff. This makes it really hard to debug. If you are worried about memory consumption you can always unset() variables.

    Also note that you should never ever have variables names $var#. Your variablenames should describe what it holds.

    In the end of the day it’s all about minimizing the number of WTFs inyour code. And option two is one big WTF.

    Does it depend on the memory size of the existing variable? I.e., is re-assigning memory more computationally expensive that assigning a new variable?

    It’s about limiting the number of WTFs for both you and other people (re)viewing your code.

    Is this always a scope issue, meaning you should use Option#2 only if you don’t need each of the variable values outside the scope shown here?

    Well if it is in a totally other scope it is fine if you use the same name multiple names. As long as it is clear what the variabel contains, e.g.:

    // perfectly fine to use the same name again. I would go as far as to say this is prefered.
    function doSomethingWithText($articleText)
    {
        // do something
    }
    
    $articleText = 'Some text of some article';
    doSomethingWithText($articleText);
    

    Does it depend on what each variable value is? Does re-assigning to different data types have different costs associated with it?

    Not a matter of cost, but a matter of maintainability. Which is often way more important.

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

Sidebar

Related Questions

First of all, apologize because I have seen some posts about this, but I
First of I apologize if this is a dumb question - I'm just starting
First, let me apologize if this is the world's stupidest question. But, I'm stumped
First off, I apologize if this has been asked before. I can't seem to
First, new to Cocoa and Objective-C, so my apologies if this is unclear and
First, I apologize if this is not an appropriate venue to ask this question,
First of all, I apologize if this is a stupid question, I'm a dead
First of all, I would like to apologize for posting this again. I am
First of all I apologize in advance for this question, a bit off the
Just want to let everyone know this is the first app I am trying

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.