scripting - Any language: recycling Variables -


this seems simple question ask but, idea re-use variables in scripting language?

i'm particularly interested in reasons why is/isn't practice i'm besides self if should or shouldn't doing it.

for example, $i 1 of common variables used in loop iterate through things.

e.g (in php):

//this script iterate each entry in $array  //and output comma if isn't last item.  $i=0;    //recycling $i  foreach ($array $v) {     $i++;      if ($i<count($array))     {         echo $v.', ';     }     else {         echo $v;     } } 

say had several loops in script, better re-use $i or use variable such $a , other loops go $b $z.

obviously re-use $i, i'd have set $i=0; or null before loop, or else give weird behaviour later on in script. makes me wonder if reusing worth hassle..

would following take more space using variable?

$i=0;    //recycling $i 

of course simple example of use , know if script terribly more complicated, cause more trouble it's worth?

i know re-using variable save minuscule amount of memory when variables stack up, gets important, doesn't it?

thank straight answers in advance , if question vague, apologize , know too- know should ask questions such these elsewhere.

resetting variable ($i=0) not going take more space.

when declare variable (say of type integer), predefined amount of memory allocated integer regardless of value is. changing zero, modify value.

if declare new variable, memory have allocated , typically initialized zero, resetting existing variable isn't taking cpu time either.

reusing index variable mutually exclusive loops idea; makes code easier understand. if you're declaring lot of variables, other people reading code may wonder if "i" still being used after loop has completed.

reuse it! :)


Comments

Popular posts from this blog

android - Spacing between the stars of a rating bar? -

aspxgridview - Devexpress grid - header filter does not work if column is initially hidden -

c# - How to execute a particular part of code asynchronously in a class -