p2p.wrox.com Forums

Need to download code?

View our list of code downloads.


  Return to Index  

beginning_php thread: problem with static variables not incrementing


Message #1 by "myles courtney" <myles_courtney@h...> on Thu, 6 Feb 2003 00:08:07
Hello out there! Is there anyone who can tell me why my static counters 
won't work. There's no reference to the keyword 'static' in my help files 
either.
Is this a new feature because I see where it isn't used in other 
references.
thanks

myles
Message #2 by "Nikolai Devereaux" <yomama@u...> on Wed, 5 Feb 2003 16:53:29 -0800
> Hello out there! Is there anyone who can tell me why my
> static counters won't work. There's no reference to the
> keyword 'static' in my help files either.
> Is this a new feature because I see where it isn't used
> in other references.


Can you give a little more detail?  What version of PHP are you running?  Can
you paste some of your code and describe more clearly what's [not] happening?


For more information on static variables, read the documentation section for
Variable Scope:
  http://www.php.net/variables.scope

Take care,

Nik

Message #3 by "myles courtney" <myles_courtney@h...> on Thu, 6 Feb 2003 23:17:04
> 
> Hello out there! Is there anyone who can tell me why my
> static counters won't work. There's no reference to the
> keyword 'static' in my help files either.
> Is this a new feature because I see where it isn't used
> in other references.


Can you give a little more detail?  What version of PHP are you running?  
Can
you paste some of your code and describe more clearly what's [not] 
happening?


For more information on static variables, read the documentation section 
for
Variable Scope:
  http://www.php.net/variables.scope

Take care,

Nik

Yes, certainly. The code is just from the book and I'm running PHP4.3.0.0 
under Apache 1.3 using maguma standard as my text editor. 

 Hope you can spot the error.

<!doctype html public "-//W3C//DTD HTML 4.0 //EN">
<html>
<head>
       <title>Scope Life</title>
</head>
<body>
<BR>
<BR>
<FONT SIZE = -1>

<?php
$GlobalVariable = "Global";

function local( ) {
    $LocalVariable = "Local";
    static $StaticVariable = 0;
    
    echo "<BR>The contents of the GlobalVariable are " . $GLOBALS
["GlobalVariable"];
    echo "<BR>The contents of the LocalVariable are $LocalVariable";
    echo "<BR>The contents of the StaticVariable are $StaticVarible";
    return(++$StaticVariable);
}

echo "<B>Calling Our function for the first time...</B>";

local();

echo "<BR><BR><B>Outside the function again...</B>";
echo "<BR>The contents of GlobalVariable are $GlobalVariable";
echo "<BR>The contents of LocalVariable are $LocalVarible";
echo "<BR>The contents of StaticVariable are $StaticVariable";
echo "<BR><BR><B>Calling Our function for the second time....</B>";

local( );

echo "<BR><BR><B>Outside the function again...</B>";
echo "<BR>The contents of GlobalVariable are $GlobalVariable";
echo "<BR>The contents of LocalVariable are $LocalVarible";
echo "<BR>The contents of StaticVariable are $StaticVariable";
echo "<BR><BR><B>Calling Our function for the third time....</B>";

local( );

echo "<BR><BR><B>Outside the function again...</B>";
echo "<BR>The contents of GlobalVariable are $GlobalVariable";
echo "<BR>The contents of LocalVariable are $LocalVarible";
echo "<BR>The contents of StaticVariable are $StaticVariable";

?>
</FONT>
</body>
</html>

Very Gratefully 

myles
Message #4 by "Nikolai Devereaux" <yomama@u...> on Thu, 6 Feb 2003 16:20:17 -0800
Problem solved:

> function local( ) {
>     $LocalVariable = "Local";
>     static $StaticVariable = 0;
>
>     echo "<BR>The contents of the GlobalVariable are "
>          . $GLOBALS["GlobalVariable"];
>     echo "<BR>The contents of the LocalVariable are $LocalVariable";
>     echo "<BR>The contents of the StaticVariable are $StaticVarible";
>     return(++$StaticVariable);
> }


You misspelled "Variable" in "StaticVariable" inside the local() function.
It's also misspelled several times outside of the function in your other echo
stmts.


take care,

nik

Message #5 by "myles courtney" <myles_courtney@h...> on Fri, 7 Feb 2003 19:50:46
> 
Problem solved:

> function local( ) {
>     $LocalVariable = "Local";
>     static $StaticVariable = 0;
>
>     echo "<BR>The contents of the GlobalVariable are "
>          . $GLOBALS["GlobalVariable"];
>     echo "<BR>The contents of the LocalVariable are $LocalVariable";
>     echo "<BR>The contents of the StaticVariable are $StaticVarible";
>     return(++$StaticVariable);
> }


You misspelled "Variable" in "StaticVariable" inside the local() function.
It's also misspelled several times outside of the function in your other 
echo
stmts.


take care,

nik



Hm, should have washed my face or gone for that fish and chips.

Thanks

myles
Message #6 by "Nikolai Devereaux" <yomama@u...> on Fri, 7 Feb 2003 17:58:11 -0800
> Hm, should have washed my face or gone for that fish and chips.

I had a tall, cold Guinness.  To each their own!  =)


  Return to Index