PHP recursive invocation and use of static variables

Source: Internet
Author: User
Tags cos php programming
  1. Draw a very beautiful leaf.
  2. Defines the value of the angle of a PI point
  3. Define ("PII", m_pi/180);
  4. Creates a new image resource and defines its background as white with a foreground color of black
  5. $im = Imagecreate (670,500);
  6. $white = Imagecolorallocate ($im, 0xFF, 0xFF, 0xFF);
  7. $g = Imagecolorallocate ($im, 0x00, 0x00, 0x00);
  8. From the code instantiated below you can tell that the initial value $x, $y, $L, $a not divided into 300, 500, 100, 270
  9. function Drawleaf ($g, $x, $y, $L, $a) {
  10. Global $im;
  11. $B = 50;
  12. $C = 9;
  13. $s 1 = 2;
  14. $s 2 = 3;
  15. $s 3 = 1.2;
  16. if ($L > $s 1) {
  17. Calculate the positioning of the leaves above
  18. $x 2 = $x + $L * cos ($a * PII);
  19. $y 2 = $y + $L * sin ($a * PII);
  20. $x 2R = $x 2 + $L/$s 2 * cos (($a + $B) * PII);
  21. $y 2R = $y 2 + $L/$s 2 * sin (($a + $B) * PII);
  22. $x 2L = $x 2 + $L/$s 2 * cos (($a-$B) * PII);
  23. $y 2L = $y 2 + $L/$s 2 * sin (($a-$B) * PII);
  24. Calculate the position of the leaves below
  25. $x 1 = $x + $L/$s 2 * cos ($a * PII);
  26. $y 1 = $y + $L/$s 2 * sin ($a * PII);
  27. $x 1L = $x 1 + $L/$s 2 * cos (($a-$B) * PII);
  28. $y 1L = $y 1 + $L/$s 2 * sin (($a-$B) * PII);
  29. $x 1R = $x 1 + $L/$s 2 * cos (($a + $B) * PII);
  30. $y 1R = $y 1 + $L/$s 2 * sin (($a + $B) * PII);
  31. Do not draw the leaves of the trunk and foliage
  32. Imageline ($im, (int) $x, (int) $y, (int) $x 2, (int) $y 2, $g);
  33. Imageline ($im, (int) $x 2, (int) $y 2, (int) $x 2R, (int) $y 2R, $g);
  34. Imageline ($im, (int) $x 2, (int) $y 2, (int) $x 2L, (int) $y 2L, $g);
  35. Imageline ($im, (int) $x 1, (int) $y 1, (int) $x 1L, (int) $y 1L, $g);
  36. Imageline ($im, (int) $x 1, (int) $y 1, (int) $x 1R, (int) $y 1R, $g);
  37. Recursively calls itself again
  38. Drawleaf ($g, $x 2, $y 2, $L/$s 3, $a + $C);
  39. Drawleaf ($g, $x 2R, $y 2R, $L/$s 2, $a + $B);
  40. Drawleaf ($g, $x 2L, $y 2L, $L/$s 2, $a-$B);
  41. Drawleaf ($g, $x 1L, $y 1L, $L/$s 2, $a-$B);
  42. Drawleaf ($g, $x 1R, $y 1R, $L/$s 2, $a + $B);
  43. }
  44. }
  45. Instantiation of
  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 referenced in the PHP manual. Hopefully, the following code will be more useful for 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 a number from 1 to 10. When the Static_function function is run for the second time, the variable i is still not released because it is a static variable, and thus the self-increment value can be obtained.

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.