#1

  1. Join Date
    Sep 2013
    Location
    Lahore, Pakistan
    Posts
    74
    Rep Power
    1

    Talking Static Function : More better


    I made a simple
    Static Function.

    can anyone tell me more?

    what is static variable use actually?
    what is its benefits?
    teach me a good simple static function.

    my code is :
    PHP Code:
    <?php  
        
    echo "<p>My Computer getting old each year.</p>";  
        function 
    age()
            {  
            static 
    $age 1;  
            echo 
    "Computer age update: "$age "<br />"//dot operator to used HTML tag.
            
    $age++;  
            }  
        
    age();  
        
    age();  
        
    age();  
     
    ?>
  2. #2
  3. Lord of the Dance
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Oct 2003
    Posts
    3,663
    Rep Power
    1958
    there is a description of static in the PHP documentation and at this (near example 4).


    Basically, static means the variable/function only exists once in the whole process and is "shared" across all instances.
    Last edited by MrFujin; September 29th, 2013 at 05:09 PM.
  4. #3

  5. Join Date
    Sep 2013
    Location
    Lahore, Pakistan
    Posts
    74
    Rep Power
    1

    Thumbs up


    Originally Posted by MrFujin
    Basically, static means the variable/function only exists once in the whole process and is "shared" across all instances.
    friend that i know; my teacher told me it.

    but i checked php.net site, too much data, php.net makes me confuse, because I am newbie,

    php.net used simple class function itself.

    and my teacher did not teach me class still.

    it is like

    I wrote here, which i learned in Static Variable in PHP post.

    give me simple simple concept that i can try in wampserver.
  6. #4
  7. Lord of the Dance
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Oct 2003
    Posts
    3,663
    Rep Power
    1958
    Looking at the code you have posted.

    When you call age() three times, it should have this output:
    Code:
    Computer age update: 1
    Computer age update: 2
    Computer age update: 3
    This is because of the static, which let it "re-use" the same variable for each call.

    If you didn't specify age with static keyword, it would "re-create" the variable every time you call the function. The output - without static - would therefore be
    Code:
    Computer age update: 1
    Computer age update: 1
    Computer age update: 1
  8. #5

  9. Join Date
    Sep 2013
    Location
    Lahore, Pakistan
    Posts
    74
    Rep Power
    1
    Originally Posted by MrFujin
    Code:
    Computer age update: 1
    Computer age update: 1
    Computer age update: 1
    great thing you told me; my teacher forgot to tell me this. lolx.
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2009
    Posts
    677
    Rep Power
    7
    echo "Computer age update: ". $age . "<br />"; //dot operator to used HTML tag.
    Just curious about your comment after the code... If you are referencing the period sign needed because of the HTML, this is false. It is more a connecting item, but can be to anything.
    PHP Code:
    echo 'Yesterday, when ' $firstName $lastName ' went to the supermarket...'
    Now, the one thing that doesn't make this the best example is the fact that there will be no space between the person's first and last name, but I was just to offer a view that the . just seperates/connects multiple items within the same line.
    He who knows not and knows not he knows not: he is a fool - shun him. He who knows not and knows he knows not: he is simple - teach him. He who knows and knows not he knows: he is asleep - wake him. He who knows and knows he knows: he is wise - follow him
  12. #7

  13. Join Date
    Sep 2013
    Location
    Lahore, Pakistan
    Posts
    74
    Rep Power
    1

    :d


    okay sir i'll remind it.

IMN logo majestic logo threadwatch logo seochat tools logo