PHP recursive algorithm-PHP Tutorial

Source: Internet
Author: User
Describes PHP recursive algorithms. PHP is still quite common, so I studied the PHP recursive algorithm and shared it with you here. I hope it will be useful to you. PHP, a nested abbreviation name, is more commonly used than PHP in English, so I studied the PHP recursive algorithm and shared it with you here, I hope it will be useful to you. PHP, an abbreviated name for nesting, is the abbreviation of the English Super Text preprocessing language (PHP: Hypertext Preprocessor. PHP is an embedded HTML language. it is a scripting language that executes HTML documents embedded on the server. the language style is similar to the C language, it is widely used by many website programmers. The unique PHP syntax is a mix of C, Java, Perl, and PHP self-innovative syntaxes.

It can execute dynamic web pages more quickly than CGI or Perl. Compared with other programming languages, PHP embeds programs into HTML documents for execution. the execution efficiency is much higher than the CGI that generates HTML tags completely; compared with the scripting language JavaScript, which is also embedded in HTML documents, PHP is executed on the server, making full use of the server performance; the PHP execution engine also stores frequently accessed PHP programs in the memory. when other users access this program again, they do not need to re-compile the program, you only need to directly execute the code in the memory, which is also a reflection of PHP's high efficiency.

PHP has very powerful functions, all CGI or JavaScript functions can be implemented in PHP, and supports almost all popular databases and operating systems. Here we will introduce PHP recursive algorithms in detail.

PHP recursive algorithm code:

 
 
  1. Php
  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. FunctiondrawLeaf ($ 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 my personal PHP programming experience, 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.
  50. Header ("Content-type: text/plain ");
  51. Functionstatic_function (){
  52. Static $I=0;
  53. If ($ I ++<10){
  54. Echo $ I. "n ";
  55. Static_function ();
  56. }
  57. }
  58. Static_function ();

This code outputs numbers 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.


Bytes. PHP, an abbreviated name for nesting, is an English super...

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.