Home >Backend Development >PHP Tutorial >PHP recursive calls and the use of static variables

PHP recursive calls and the use of static variables

WBOY
WBOYOriginal
2016-07-25 09:03:521347browse
  1. //Draw a very beautiful leaf
  2. //Define the value of one minute of PI angle
  3. define("PII", M_PI/180);
  4. //Create a new image resource and define it The background is white and the foreground color is black
  5. $im = imagecreate(670,500);
  6. $white = imagecolorallocate($im, 0xFF, 0xFF, 0xFF);
  7. $g = imagecolorallocate($im, 0x00, 0x00, 0x00);
  8. // From the instantiated code below, we can know that the initial values ​​$x, $y, $L, $a are divided into 300, 500, 100, 270 respectively
  9. function drawLeaf($g, $x, $y, $ L, $a) {
  10. global $im;
  11. $B = 50;
  12. $C = 9;
  13. $s1 = 2;
  14. $s2 = 3;
  15. $s3 = 1.2;
  16. if($L > $s1 ) {
  17. // Calculate the positioning of the leaves above
  18. $x2 = $x + $L * cos($a * PII);
  19. $y2 = $y + $L * sin($a * PII);
  20. $x2R = $x2 + $L / $s2 * cos(($a + $B) * PII);
  21. $y2R = $y2 + $L / $s2 * sin(($a + $B) * PII);
  22. $ x2L = $x2 + $L / $s2 * cos(($a - $B) * PII);
  23. $y2L = $y2 + $L / $s2 * sin(($a - $B) * PII);
  24. // Calculate the positioning of the leaves below
  25. $x1 = $x + $L / $s2 * cos($a * PII);
  26. $y1 = $y + $L / $s2 * sin($a * PII);
  27. $x1L = $x1 + $L / $s2 * cos(($a - $B) * PII);
  28. $y1L = $y1 + $L / $s2 * sin(($a - $B) * PII );
  29. $x1R = $x1 + $L / $s2 * cos(($a + $B) * PII);
  30. $y1R = $y1 + $L / $s2 * sin(($a + $B) * PII);
  31. // Draw the main stem and leaf surface of the leaf respectively
  32. ImageLine($im, (int)$x, (int)$y, (int)$x2, (int)$y2, $g);
  33. ImageLine($im, (int)$x2, (int)$y2, (int)$x2R, (int)$y2R, $g);
  34. ImageLine($im, (int)$x2, (int)$ y2, (int)$x2L, (int)$y2L, $g);
  35. ImageLine($im, (int)$x1, (int)$y1, (int)$x1L, (int)$y1L, $g );
  36. ImageLine($im, (int)$x1, (int)$y1, (int)$x1R, (int)$y1R, $g);
  37. // Call itself recursively again
  38. drawLeaf($g, $ x2, $y2, $L / $s3, $a + $C);
  39. drawLeaf($g, $x2R, $y2R, $L / $s2, $a + $B);
  40. drawLeaf($g, $ x2L, $y2L, $L / $s2, $a - $B);
  41. drawLeaf($g, $x1L, $y1L, $L / $s2, $a - $B);
  42. drawLeaf($g, $ x1R, $y1R, $L / $s2, $a + $B);
  43. }
  44. }
  45. // Instantiate
  46. drawLeaf($g, 300, 500, 100, 270);
  47. header("Content-type: image/png");
  48. imagepng($im);
  49. ?>
Copy code

In PHP programming, recursive calls are often used with static variables. The meaning of static variables can be found in the PHP manual. I hope the following code will be more conducive to understanding recursion and static variables.

  1. header("Content-type: text/plain");
  2. function static_function () {
  3. static $i = 0;
  4. if ($i++ < 10) {
  5. echo $ i . "n";
  6. static_function();
  7. }
  8. }
  9. static_function();
Copy code

The above code will output numbers from 1 to 10. When the static_function function runs for the second time, the variable i is still retained and not released because it is a static variable, and the auto-incremented value can be obtained.



Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn