Static Variables

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
WebbDawg
Forum Newbie
Posts: 2
Joined: Sat Jan 11, 2003 10:29 pm

Static Variables

Post 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.
oldtimer
Forum Contributor
Posts: 204
Joined: Sun Nov 03, 2002 8:21 pm
Location: Washington State

Post by oldtimer »

You can always write it to a file.
User avatar
Elmseeker
Forum Contributor
Posts: 132
Joined: Sun Dec 22, 2002 5:48 am
Location: Worcester, MA

Post 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? ;)
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post 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++;
}
;)
Post Reply