SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Enthusiast
    Join Date
    Dec 2004
    Location
    Japan
    Posts
    79
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    The use of Static Variable

    Hi,

    I'm having trouble using a static variable. I expected that following code echoes 12, which is the number of called elements. I think I have a wrong way of using the static. In order to get 12, where has to be fixed?

    PHP Code:
    <?php
    $a 
    0;

    Test();
    Test();
    Test();

    echo 
    $a;

    function 
    Test()
    {
       static 
    $a 0;
       
    $arr = array(abcd);
       foreach (
    $arr as $num) {
       
    $a++;
       }
    }
    ?>
    Any information would be greatly appreaciated.

  2. #2
    SitePoint Wizard stereofrog's Avatar
    Join Date
    Apr 2004
    Location
    germany
    Posts
    4,324
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Static variables are only accessible inside functions they are declared in. There is no way to fix your code with static vars, you need a global non-static variable:

    PHP Code:
    function Test()
    {
       global 
    $a;
       foreach (
    $arr as $num) {
       
    $a++;
       }


  3. #3
    SitePoint Enthusiast
    Join Date
    Dec 2004
    Location
    Japan
    Posts
    79
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank you so much, stereofrog. I really appreciate it.

  4. #4
    SitePoint Wizard silver trophy kyberfabrikken's Avatar
    Join Date
    Jun 2004
    Location
    Copenhagen, Denmark
    Posts
    6,157
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Keep in mind that global variables are a bad way of sharing information. You should pass input data in as parameters, and output data out with return. So for example, you could do this:
    PHP Code:
    $a 0;

    $a Test($a);
    $a Test($a);
    $a Test($a);

    echo 
    $a;

    function 
    Test($a)
    {
        
    $arr = array("a""b""c""d");
        foreach (
    $arr as $num) {
            
    $a++;
        }
        return 
    $a;


  5. #5
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    Passing in $a as a reference also works... but is that what you want?

    PHP Code:
    $a ;

    test($a);
    test($a);
    test($a);

    function 
    Test( &$a ){

    $b = array('a' ,'b''c' 'd');

    foreach(
    $b as $c)
    $a++;

    return 
    $a;


    }

    echo 
    $a//  PHP5 returns 12 anyway. 

  6. #6
    @php.net Salathe's Avatar
    Join Date
    Dec 2004
    Location
    Edinburgh
    Posts
    1,397
    Mentioned
    64 Post(s)
    Tagged
    0 Thread(s)
    Why not just return $a ?
    PHP Code:
    <?php

    Test
    ();
    Test();
    echo 
    Test();

    function 
    Test()
    {
       static 
    $a 0;
       
    $arr = array('a''b''c''d');
       foreach (
    $arr as $num) {
          
    $a++;
       }
       return 
    $a;
    }
    ?>

  7. #7
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    >Why not just return $a ?

    Because at run time $a might be 17?

    I think it all depends what the OP wants to do with $a and why....

  8. #8
    SitePoint Enthusiast
    Join Date
    Dec 2004
    Location
    Japan
    Posts
    79
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks everyone for sharing nice ways of doing it. The use of return was something I could not think about.


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
  •