Php recursive call and use of static variables

Source: Internet
Author: User
Php recursive call and use of static variables

  1. // Draw a beautiful leaf
  2. // Define the point of view of PI
  3. Define ("PII", M_PI/180 );
  4. // Create an image resource and set the background to White. the foreground color to black.
  5. $ Im = imagecreate (670,500 );
  6. $ White = imagecolorallocate ($ im, 0xFF, 0xFF, 0xFF );
  7. $ G = imagecolorallocate ($ im, 0x00, 0x00, 0x00 );
  8. // The Code instantiated below shows that the initial values $ x, $ y, $ L, and $ a are not divided into 300,500,100,270.
  9. Function drawLeaf ($ g, $ x, $ y, $ L, $ ){
  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 leaf positioning 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 position of the leaf.
  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. // Separate the trunk and leaf area of the leaves
  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. // Re-recursive call itself
  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. ?>

In php programming, recursive calls are often used with static variables. For more information about static variables, see The PHP Manual. We hope the following code will be more conducive to understanding recursive 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 ();

The above code outputs a number ranging from 1 to 10. During the second operation of the static_function, variable I is retained and cannot be released because it is a static variable, so that the auto-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.