SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Enthusiast Jujubee's Avatar
    Join Date
    Mar 2001
    Location
    Canada
    Posts
    98
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    "Static" keyword does what?

    I was browsing PHP.NET and came across a snippet of code:

    Code:
    static $foo= 45;
    I can't find any reference to "static" at PHP.net. Does anyone know what it does?

    Thanks.

  2. #2
    SitePoint Wizard
    Join Date
    Jul 1999
    Location
    Chicago
    Posts
    2,629
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi, this page has a good explanation about 3 pages down. Basically, they keep their value each time the function they're in is executed.

    So:

    PHP Code:
    $one static_var_example();
    $two static_var_example();
    echo 
    "$one $two"# prints "1 2"
    function static_var_example() {
            static 
    $a;
            if(!
    is_integer($a)) {$a=0}
            
    $a++;
            return 
    $a;


  3. #3
    SitePoint Enthusiast Jujubee's Avatar
    Join Date
    Mar 2001
    Location
    Canada
    Posts
    98
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ok, thanks for the link.

    What are some uses for this?

    And what's the difference between this and declaring a variable as "global" in the function?

  4. #4
    SitePoint Wizard creole's Avatar
    Join Date
    Oct 2000
    Location
    Nashvegas Baby!
    Posts
    7,845
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Global allows a variable to be used inside any funtion on a page. This means that ANY function can change the value of the variable.

    Based on Quinns description, I would assume that a variable that is static does not change at all. Sort of like a locked variable.

    Someone let me know if I'm blowing smoke.
    Adobe Certified Coldfusion MX 7 Developer
    Adobe Certified Advanced Coldfusion MX Developer
    My Blog (new) | My Family | My Freelance | My Recipes

  5. #5
    Making a better wheel silver trophy DR_LaRRY_PEpPeR's Avatar
    Join Date
    Jul 2001
    Location
    Missouri
    Posts
    3,428
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    http://www.php.net/manual/en/languag...bles.scope.php

    that page in the manual describes it. basically it lets you call a function recursively without the variable being reset each time. so, if you say static $var = 0; it will only be set to 0 the first call and then when you change its value in the function it will be "remembered" the next time. it's useful if you want some kind of counter incremented each time.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •