Static variable - PHP Online

Form of PHP Sandbox

Enter Your PHP code here for testing/debugging in the Online PHP Sandbox. As in the usual PHP files, you can also add HTML, but do not forget to add the tag <?php in the places where the PHP script should be executed.



Your result can be seen below.

Result of php executing





Full code of Static variable.php

  1. <?php
  2.  
  3.  
  4. function sumValues($a, $b)
  5. {
  6.     static $count = 0;
  7.     $count++;
  8.     if($count > 2)
  9.     {
  10.         return false;
  11.     } 
  12.     else
  13.     {
  14.         return [$a + $b, $count];
  15.     }
  16. }
  17.  
  18. print_r(sumValues(3, 4));
  19. echo '<br>';
  20. print_r(sumValues(6, 7));
  21. echo '<br>';
  22. print_r(sumValues(2, 5));
  23. echo '<br>';
  24. print_r(sumValues(1, 3));
  25.  
  26.  
File Description
  • Static variable
  • PHP Code
  • 26 Oct-2019
  • 330 Bytes
You can Share it: