Page 1 of 1

Static Variables

Posted: Sat Jan 11, 2003 10:29 pm
by WebbDawg
I'm new to php and have used VisualBasic. Is there a Static type variable in php to hold its value (like a totalizer). I want to just count without having to use a db to hold a value. If you want to respond directly to me. thedog@webbdawg.com

Thanks.

Posted: Sat Jan 11, 2003 10:33 pm
by oldtimer
You can always write it to a file.

Posted: Sun Jan 12, 2003 8:53 am
by Elmseeker
Hmmmm...what would be the point of creating static variables?! I see it this way:

Static == Unchanging.
Unchanging != Variable.

I mean if it doesn't change it's not exactly "variable" anymore is it? ;)

Posted: Sun Jan 12, 2003 9:07 am
by volka
static has a slightly different meaning in this context, although Static == Unchanging points to the right direction
MSDN VB library wrote:In addition to scope, variables have a lifetime
[...]
The next time the procedure is executed, all its local variables are reinitialized.
However, you can preserve the value of a local variable by making the variable static. Use the Static keyword to declare one or more variables inside a procedure, exactly as you would with the Dim statement:
Static Depth
For example, the following function calculates a running total by adding a new value to the total of previous values stored in the static variable Accumulate:

Function RunningTotal(num)
Static ApplesSold
ApplesSold = ApplesSold + num
RunningTotal = ApplesSold
End Function
and php offers the same
http://www.php.net/manual/en/language.variables.scope.php#AEN4516 wrote:<?php
function Test()
{
static $a = 0;
echo $a;
$a++;
}
;)