php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #16876 Static PHP Variables
Submitted: 2002-04-27 13:19 UTC Modified: 2002-04-27 14:10 UTC
From: tonicpeddler at cs dot com Assigned:
Status: Not a bug Package: Feature/Change Request
PHP Version: 4.2.0 OS: Windows XP
Private report: No CVE-ID: None
View Add Comment Developer Edit
Anyone can comment on a bug. Have a simpler test case? Does it work for you on a different platform? Let us know!
Just going to say 'Me too!'? Don't clutter the database with that please !
Your email address:
MUST BE VALID
Solve the problem:
9 + 25 = ?
Subscribe to this entry?

 
 [2002-04-27 13:19 UTC] tonicpeddler at cs dot com
I think some php users might find it useful if you could declare a variable as a static variable.  When the script finishes executing, PHP rewrites the original declaration of those variables to the values they had at the end of script execution.

Maybe I'm wrong, or maybe there's a more effective way to accomplish this, but there is certainly a very obvious and simple syntax for such a feature.

static $var = value;

It would certainly simplify a lot for me, and probably at least a few other people.

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2002-04-27 13:39 UTC] mfischer@php.net
Please be more specific what you mean. There's already a 'static' modifier for variables within function if you havent spotted it yet. Feel free to re-open the report if you can provide a more clearer description what the goal of your 'static' version should be.
 [2002-04-27 13:49 UTC] tonicpeddler at cs dot com
My suggestion is a feature that could be used for instance, to remember settings without accessing a file or a database to find out what those settings are.  For instance, something like this:

example.php
<?php
       STATIC VARIABLE $fontcolor = '#0000FF';

       echo <<<EOT
<html>
<body text="$fontcolor">
Hello, this is the color
 [2002-04-27 13:53 UTC] tonicpeddler at cs dot com
My suggestion is a feature that could be used for instance, to remember settings without accessing a file or a database to find out what those settings are.  For instance, something like this:

example.php
<?php
       STATIC VARIABLE $fontcolor = 'white';
       if ( $newcolor != "" ) {
               $fontcolor = $newcolor;
               $message = "Congratulations on your new color!"; }
       echo <<<EOT
<html>
<body text="$fontcolor">
<p align=center>
Hello, this is the color.
You may choose a new color like 
<a href="example.php?newcolor=red">Red</a>.
</p>
</body>
</html>
EOT;

// end of script
?>
 [2002-04-27 13:56 UTC] tonicpeddler at cs dot com
I accidently pressed the submit button prematurely, one of those posts is quite obviously incomplete, and repeats the beginning of the comment directly after proceeding it.
 [2002-04-27 14:10 UTC] mfischer@php.net
This feature you're requesting is called sessions, see http://php.net/session/
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Mar 28 17:01:29 2024 UTC