Static variable problem PHP

  • Madzmar
  • Newbie
  • Newbie
  • User avatar
  • Posts: 8

Post 3+ Months Ago

Hi... I would like to ask if anyone of you knows what was the problem with my script... here the situation...

i created 3 files in php namely index.php, function.php, secondpage.php

i created a function in function.php that increments the static variable count...
and in the index.php this file calls that functions and increment the value of count 3x... so i was expecting that the value of count would be 3... this is true when i evaluate it in the page index.php... but when i call the secondpage.php using a link in the index.php it seems that my static variable count looses its value...

heres the code in index.php

PHP Code: [ Select ]
<?php
   include("function.php");
   include("secondpage.php");
?>
<head>
<title>Index page</title>
</head>
<body>
<?php
  Function::incrementCount();
  Function::incrementCount();
  Function::incrementCount();
 
<a href= "secondpage.php">second page</a>
 
?>
 
</body>
  1. <?php
  2.    include("function.php");
  3.    include("secondpage.php");
  4. ?>
  5. <head>
  6. <title>Index page</title>
  7. </head>
  8. <body>
  9. <?php
  10.   Function::incrementCount();
  11.   Function::incrementCount();
  12.   Function::incrementCount();
  13.  
  14. <a href= "secondpage.php">second page</a>
  15.  
  16. ?>
  17.  
  18. </body>



and heres the code for the function.php


PHP Code: [ Select ]
<?php
class Function{
 
   public static $count;
   public static function incrementCount(){
           self::$count++;
 
   }
}
?>
  1. <?php
  2. class Function{
  3.  
  4.    public static $count;
  5.    public static function incrementCount(){
  6.            self::$count++;
  7.  
  8.    }
  9. }
  10. ?>



and heres the code for the secondpage.php


PHP Code: [ Select ]
/* in this file it simply display the value of the static variable $count */
<?php
    include("function.php");
?>
<?php
  echo Function::$count;
 
?>
  1. /* in this file it simply display the value of the static variable $count */
  2. <?php
  3.     include("function.php");
  4. ?>
  5. <?php
  6.   echo Function::$count;
  7.  
  8. ?>



in the index.php file the value of the static variable works fine but when i switch to another page which is the secondpage.php the value of that static variable is being reset... could someone help me solve my problem if you have answer for my problem i would greatly appreciate it
  • Anonymous
  • Bot
  • No Avatar
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post 3+ Months Ago

  • joebert
  • Fart Bubbles
  • Genius
  • User avatar
  • Posts: 13502
  • Loc: Florida

Post 3+ Months Ago

By default PHP does not save application state between requests made by the visitor. When they click that link, the application state is basically being thrown away and it starts all over again when they get to the next page.

On a per-visitor bases, you can use sessions to maintain your application state.

If you've meant for that static variable to remain static between all requests made by all visitors you'll need to implement some sort of database, flat-file, or memcached based system to maintain your applications state.

Post Information

  • Total Posts in this topic: 2 posts
  • Users browsing this forum: No registered users and 83 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
cron
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.