Go Back  HTML Forums - Free Webmaster Forums and Help Forums > WEBSITE DEVELOPMENT > Server Side Programming > PHP Programming
User Name:
Password:
 

Reply
Thread Tools   Display Modes
  View First Unread
 
Old 10-19-2009, 08:31 AM
  #1
DamianWarS
Myrmidon (Level 12)
 
DamianWarS's Avatar
 
Join Date: Jan 2007
Posts: 170
iTrader: (0)
DamianWarS is an unknown quantity at this point
keyword to set variable inside loop only once

i want to be able to declare and set a variable inside a while loop but only once. So the first time the loop runs a variable is set but each additional time it isn't reset to that value. I know there is a keyword to tell a variable to do this but I can't remember it and I can't find it anywhere.... does anyone know what i'm talking about and what this keyword is?
DamianWarS is offline   Add to del.icio.us Add to del.icio.us    Can you digg it?Can you digg it? Reply With Quote
Old 10-19-2009, 10:29 PM
  #2
Horus_Kol
Mod of the Underlay
 
Horus_Kol's Avatar
 
Join Date: Jun 2002
Location: At a desk, hooked up and ready to rock
Posts: 17,242
iTrader: (0)
Horus_Kol is a glorious beacon of lightHorus_Kol is a glorious beacon of lightHorus_Kol is a glorious beacon of lightHorus_Kol is a glorious beacon of lightHorus_Kol is a glorious beacon of light
set a flag outside of the loop:

PHP Code:
$done false;

for (
$i 0$i 10$i++) {

  if (!
$done) {
    
dosomething();
    
$done true;
  } 
}
__________________
Personal Blog (and photos): HorusKol
Articles on Programming and Development (PHP/HTML/CSS, C/C++, more): RandomTweak

The great secret that no SEO agent wants you to hear: if you build your website using w3c accessibility guidelines and your content is written for people, you will do better for longer in search engines than any other method...

Last edited by Horus_Kol : 10-21-2009 at 01:54 AM.
Horus_Kol is offline   Add to del.icio.us Add to del.icio.us    Can you digg it?Can you digg it? Reply With Quote
Old 10-20-2009, 12:59 PM
  #3
DamianWarS
Myrmidon (Level 12)
 
DamianWarS's Avatar
 
Join Date: Jan 2007
Posts: 170
iTrader: (0)
DamianWarS is an unknown quantity at this point
thanks for the reply but that's not what i'm talking about. After I read your post I decided to look at some old code of mine and I found my answer. the keyword is the "static" keyword and here is an example for what I want to use it for:

PHP Code:
while(1==1) {
    static 
$i0;
    
$i++;

with my example the keyword "static" initializes and sets $1 to 0 but it only does it once. the next loop it does not reset $1 back to 0 but it will continue to increment $i to the next value.
DamianWarS is offline   Add to del.icio.us Add to del.icio.us    Can you digg it?Can you digg it? Reply With Quote
Old 10-22-2009, 09:03 AM
  #4
scoutt
Mister Admin to you
 
scoutt's Avatar
 
Join Date: Jul 2001
Posts: 30,730
iTrader: (0)
scoutt is a jewel in the roughscoutt is a jewel in the roughscoutt is a jewel in the rough
DamianWarS, that doesn't even make sense. a for() loop does exactly that so why would you need static? You're whole concept of the word 'static' is not even being used in what you are using it for. static means you set a variable in a OOP (class) setup so that it makes them accessible without needing an instantiation of the class.

Your loop there is a never ending loop, it will never stop. Therefore crashing your browser.

lol, this does the exact same thing

PHP Code:
$i=0;
while(
1==1) {    
    
$i++;

__________________
Have a Script or Snippet you want to share?

WWW Standards: HTML 4.01, CSS2.1, CSS3, XHTML 1.0
PHP Standards: PHP Standards
scoutt is offline   Add to del.icio.us Add to del.icio.us    Can you digg it?Can you digg it? Reply With Quote
Old 10-22-2009, 12:24 PM
  #5
DamianWarS
Myrmidon (Level 12)
 
DamianWarS's Avatar
 
Join Date: Jan 2007
Posts: 170
iTrader: (0)
DamianWarS is an unknown quantity at this point
I know the loop is never ending but I thought it was obvious that it was just a simplified example. I also know the static keyword is not reserved for this use but even still it is working for this use. I will let you figure out what scenario would come up where this could be beneficial, otherwise you can do what you want with this.
DamianWarS is offline   Add to del.icio.us Add to del.icio.us    Can you digg it?Can you digg it? Reply With Quote

Reply
KEEP TABS
SPONSORS
 
Boxedart
 
 


 
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
  
 
 
 



 
  POSTING RULES
 
 
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
vB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Thread Tools
Display Modes

Forum Jump

 

All times are GMT -5. The time now is 11:26 PM.

   

Mascot team created by Drawshop.com

Powered by vBulletin® Version 3.6.7
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.

Server Monitoring by ENIACmonitor 0.01
HTMLforums.com © Big Resources, Inc. Web Design by BoxedArt.com
vRewrite 1.5 beta SEOed URLs completed by Tech Help Forum and Chalo Na.