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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T02:53:31+00:00 2026-05-18T02:53:31+00:00

This seems like a simple question to ask but, is it generally a good

  • 0

This seems like a simple question to ask but, is it generally a good idea to re-use variables in any scripting language?

I’m particularly interested in the reasons why it is/isn’t good practice as I’m besides my self if I should or shouldn’t be doing it.

For example, $i is one of the most common variables used in a loop to iterate through things.

E.g (in php):

//This script will iterate each entry in $array 
//and output it with a comma if it isn't the last item.

$i=0;    //Recycling $i

foreach ($array as $v) {
    $i++;

    if ($i<count($array))
    {
        echo $v.', ';
    }
    else {
        echo $v;
    }
}

Say I had several loops in my script, would it be better to re-use $i or to just use another variable such as $a and for any other loops go from $b to $z.

Obviously to re-use $i, I’d have to set $i=0; or null before the loop, or else it would give some very weird behaviour later on in the script. Which makes me wonder if reusing it is worth the hassle..

Would the following take up more space than just using another variable?

$i=0;    //Recycling $i

Of course this is the most simple example of use and I would like to know about if the script were terribly more complicated, would it cause more trouble than it’s worth?

I know that re-using a variable could save a minuscule amount of memory but when those variables stack up, it gets important, doesn’t it?

Thank you for any straight answers in advance and if this question is too vague, I apologize and would like to know that too- so that I know I should ask questions such as these elsewhere.

  • 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-18T02:53:31+00:00Added an answer on May 18, 2026 at 2:53 am

    Resetting your variable ($i=0) is not going to take more space.

    When you declare a variable (say of type integer), a predefined amount of memory is allocated for that integer regardless of what its value is. By changing it back to zero, you only modify the value.

    If you declare a new variable, memory will have to be allocated for it and typically it will also be initialized to zero, so resetting your existing variable probably isn’t taking any extra CPU time either.

    Reusing an index variable for mutually exclusive loops is a good idea; it makes your code easier to understand. If you’re declaring a lot of extra variables, other people reading your code may wonder if “i” is still being used for something even after your loop has completed.

    Reuse it! 🙂

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

Sidebar

Related Questions

This seems like a simple question, but I can't find it with the Stack
This seems like a pretty softball question, but I always have a hard time
This seems like a silly question but I would really like your comments and
This seems like perhaps a naive question, but I got into a discussion with
So, I'm not sure how to ask this question, as it seems like it
This seems like a very simple and a very common problem. The simplest example
This sure seems like a simple error to fix. However, I somehow can't figure
This seems like such a trivial problem, but I can't seem to pin how
It seems like this should be straightforward but I'm boggling. I've got my listview
I'd like ask a question about building applications in .NET that use data from

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.