diamondnular Posted July 8, 2008 Share Posted July 8, 2008 Hi folks, I am coding a page, and the page will call one php function several times. Here is how it works: <?php function display() { // check if run the first // first run task echo 'FIRSTRUN - '; // if not the first, then neglect echo 'test' . "\n"; } ?> so for example, if the function is called 5 times when the page is loaded, the result will be FIRSTRUN - test test test test test Anybody has an idea that will be possible to do? Note that the th-running check is implemented each time the page is loaded, not just the very first time when the function is called. Thanks, D. Quote Link to comment Share on other sites More sharing options...
trq Posted July 8, 2008 Share Posted July 8, 2008 <?php function display() { static $foo = false; if (!$foo) { echo "FIRSTRUN - \n"; $foo = true; } else { echo 'test' . "\n"; } } display(); display(); display(); display(); ?> Quote Link to comment Share on other sites More sharing options...
diamondnular Posted July 8, 2008 Author Share Posted July 8, 2008 <?php function display() { static $foo = false; if (!$foo) { echo "FIRSTRUN - \n"; $foo = true; } else { echo 'test' . "\n"; } } ?> Wow, it is so simple that I can not figure it out Thanks a bunch Thorpe. Just another question: can we implement this for the th-running? For example, the check will run if the th-run is third, and will not if others. Thanks, D. Quote Link to comment Share on other sites More sharing options...
trq Posted July 8, 2008 Share Posted July 8, 2008 Of course you can, a little logical thinking goes a long way. <?php function display() { static $foo = 1; if ($foo == 3) { echo "Third run\n"; } else { echo "Do nothing\n"; } $foo++; } display(); display(); display(); display(); ?> Quote Link to comment Share on other sites More sharing options...
diamondnular Posted July 8, 2008 Author Share Posted July 8, 2008 Of course you can, a little logical thinking goes a long way. <?php function display() { static $foo = 1; if ($foo == 3) { echo "Third run\n"; } else { echo "Do nothing\n"; } $foo++; } display(); display(); display(); display(); ?> Thank you very much Thorpe . D. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.