#1

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

    Post Static Variable Problem


    Static Variable Use in Function. I am getting error in this code.

    but I think everything is okay. but something is wrong.

    can any one underline it. please. and solve it.

    PHP Code:
    <?php  
                              
    echo "Age behavior with every year: <br />"
                              function 
    age()  
                              {  
                              static 
    $age 20;  
                              echo 
    "Samee age update: "$age "<br />";  
                              
    $age++;  
                              }  
                              
    age();  
                              
    age();  
                              
    age();   
                              
    ?>
    what is the problem here.
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Dec 2004
    Posts
    2,990
    Rep Power
    375
    maybe you are not allowed to have static in a function! you can in a class but not in a function!

    if you are using php 5.5 then there is a new feature called "yield".

    In your case i would just pass in a param!

    EDIT:

    actually i was wrong, you seem to have a problem in this line:

    echo "Samee age update: ". $age "<br />";

    I will let you work it out. when i fixed it, it worked for me.
  4. #3
  5. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,959
    Rep Power
    1014
    Originally Posted by same2cool
    Static Variable Use in Function. I am getting error in this code.
    What error? The error message is crucial, because it tells you (and us) exactly what's wrong.

    PHP doesn't make up errors just to annoy you. If it tells you the code is wrong, then the code is wrong. And there's a specific reason for why it's wrong.
    The 6 worst sins of security ē How to (properly) access a MySQL database with PHP

    Why canít I use certain words like "drop" as part of my Security Question answers?
    There are certain words used by hackers to try to gain access to systems and manipulate data; therefore, the following words are restricted: "select," "delete," "update," "insert," "drop" and "null".
  6. #4
  7. Sarcky
    Devshed Supreme Being (6500+ posts)

    Join Date
    Oct 2006
    Location
    Pennsylvania, USA
    Posts
    10,908
    Rep Power
    6351
    Why do you believe we can magically determine which error you're experiencing? Help us help you. Share the errors you're getting. There are tens of thousands of base errors in PHP, you can't just say "I am getting the error." Show us the error and explain why you believe it's related to the "static" keyword (hint: it's not, you have a basic parse error)

    Comments on this post

    • paulh1983 agrees : i agree in practice, BUT jacques already covered this ;)
    HEY! YOU! Read the New User Guide and Forum Rules

    "They that can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety." -Benjamin Franklin

    "The greatest tragedy of this changing society is that people who never knew what it was like before will simply assume that this is the way things are supposed to be." -2600 Magazine, Fall 2002

    Think we're being rude? Maybe you asked a bad question or you're a Help Vampire. Trying to argue intelligently? Please read this.
  8. #5

  9. Join Date
    Sep 2013
    Location
    Lahore, Pakistan
    Posts
    74
    Rep Power
    1
    PHP Code:
    echo "Samee age update: " $age "<br />"
    yes it should be with dot operator

IMN logo majestic logo threadwatch logo seochat tools logo