Recipe 5.5. Using Static Variables5.5.1. ProblemYou want a 5.5.2. SolutionDeclare the variable as static: function track_times_called() { 5.5.3. DiscussionDeclaring a variable static causes its value to be remembered by a function. So, if there are subsequent calls to the function, you can access the value of the saved variable. The pc_check_the_count( )
In pc_check_the_count( ), the logic of what happens to the batter depending on the pitch count is in the switch statement inside the function. You can instead return the number of strikes and balls, but this requires you to place the checks for striking out, walking, and staying at the plate in multiple places in the code. While static variables retain their values between function calls, they do so only during one invocation of a script. A static variable accessed in one request doesn't keep its value for the next request to the same page. 5.5.4. See AlsoDocumentation on static variables at http://www.php.net/language.variables.scope. |
Monday, January 25, 2010
Recipe 5.5. Using Static Variables
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment