Count number of actions taken


/ Published in: PHP
Save to your folder(s)



Copy this code and paste it in your HTML
  1. function tally($ns = null, $setTo = null) {
  2. static $tallies = array();
  3. if ($ns === null) {
  4. return $tallies;
  5. }
  6. if ($setTo) {
  7. $tallies[$ns] = $setTo;
  8. return;
  9. }
  10. if (!isset($tallies[$ns])) {
  11. $tallies[$ns] = 0;
  12. }
  13. $tallies[$ns]++;
  14. }
  15. // Example usage
  16. // foreach ($arr as $k => $v) {
  17. // tally('start of loop');
  18. // // code
  19. // if ($x) { continue; }
  20. // tally('end of loop');
  21. // }
  22. // print_r(tally()); // Array ( [start of loop] => 10, [end of loop] => 6 )

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.