majortom84 Posted June 3, 2014 Share Posted June 3, 2014 Write a function Counter() that keeps track of the number of times it is called. This function should not take any parameters and return the number of times that it has been called. Example: echo Counter();//1 echo Counter(); //2 I think I would go like this... $count = 0; function Counter() { $count = $count + 1; } Is this too simple? Am I doing it wrong Quote Link to comment https://forums.phpfreaks.com/topic/288955-count-each-time-the-function-runs/ Share on other sites More sharing options...
Jacques1 Posted June 3, 2014 Share Posted June 3, 2014 Well, you have tried it? What does it say? Quote Link to comment https://forums.phpfreaks.com/topic/288955-count-each-time-the-function-runs/#findComment-1481729 Share on other sites More sharing options...
majortom84 Posted June 3, 2014 Author Share Posted June 3, 2014 no its just keeping $count at 0 Quote Link to comment https://forums.phpfreaks.com/topic/288955-count-each-time-the-function-runs/#findComment-1481730 Share on other sites More sharing options...
majortom84 Posted June 3, 2014 Author Share Posted June 3, 2014 //$count = 0; //static $counter = 0; function Counter() { global $counter; $count = $count + 1; return $count; } echo Counter(); echo Counter(); echo Counter(); got it to ... 111 with this so i hope im getting closer. I will keep at it Quote Link to comment https://forums.phpfreaks.com/topic/288955-count-each-time-the-function-runs/#findComment-1481731 Share on other sites More sharing options...
Solution majortom84 Posted June 3, 2014 Author Solution Share Posted June 3, 2014 function Counter() { static $count; $count++; return $count; } echo Counter(); echo Counter(); echo Counter(); GOT IT !!! 123 Quote Link to comment https://forums.phpfreaks.com/topic/288955-count-each-time-the-function-runs/#findComment-1481732 Share on other sites More sharing options...
requinix Posted June 3, 2014 Share Posted June 3, 2014 Jacques1 was hoping you would reply with something like It doesn't output anything but I do get an error message about an undefined variableIf that's half true then you don't have your environment set up to tell you about problems: find your php.ini, set error_reporting = -1 display_errors = onand restart your web server. Then try the script and see what you get. There's no output because Counter() doesn't return anything to output. You get the undefined variable warnings because $count, while defined outside the function, is not defined inside the function. You don't get that variable for free. The global thing is one solution but you should avoid it. I personally would have suggested the static $count thing you have now. Quote Link to comment https://forums.phpfreaks.com/topic/288955-count-each-time-the-function-runs/#findComment-1481734 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.