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:
- Php
- // Define the point of view of PI
- Define ("PII", M_PI/180 );
-
- // Create an image resource and set the background to White. the foreground color to black.
- $Im=Imagecreate(670,500 );
- $White=Imagecolorallocate($ Im, 0xFF, 0xFF, 0xFF );
- $G=Imagecolorallocate($ Im, 0x00,0x00,0x00 );
-
- // The Code instantiated below shows that the initial values $ x, $ y, $ L, and $ a are not divided into 300,500,100,270.
- FunctiondrawLeaf ($ g, $ x, $ y, $ L, $ ){
- Global $ im;
- $B=50;
- $C=9;
- $S1=2;
- $S2=3;
- $S3=1. 2;
- If ($ L>$ S1 ){
- // Calculate the leaf positioning above
- $X2= $ X + $ L * cos ($ a * PII );
- $Y2= $ Y + $ L * sin ($ a * PII );
- $X2R= $ X2 + $ L/$ s2 * cos ($ a + $ B) * PII );
- $Y2R= $ Y2 + $ L/$ s2 * sin ($ a + $ B) * PII );
- $X2L= $ X2 + $ L/$ s2 * cos ($ a-$ B) * PII );
- $Y2L= $ Y2 + $ L/$ s2 * sin ($ a-$ B) * PII );
-
- // Calculate the position of the leaf.
- $X1= $ X + $ L/$ s2 * cos ($ a * PII );
- $Y1= $ Y + $ L/$ s2 * sin ($ a * PII );
- $X1L= $ X1 + $ L/$ s2 * cos ($ a-$ B) * PII );
- $Y1L= $ Y1 + $ L/$ s2 * sin ($ a-$ B) * PII );
- $X1R= $ X1 + $ L/$ s2 * cos ($ a + $ B) * PII );
- $Y1R= $ Y1 + $ L/$ s2 * sin ($ a + $ B) * PII );
-
- // Separate the trunk and leaf area of the leaves
- ImageLine ($ im, (int) $ x, (int) $ y, (int) $ x2, (int) $ y2, $ g );
- ImageLine ($ im, (int) $ x2, (int) $ y2, (int) $ x2R, (int) $ y2R, $ g );
- ImageLine ($ im, (int) $ x2, (int) $ y2, (int) $ x2L, (int) $ y2L, $ g );
- ImageLine ($ im, (int) $ x1, (int) $ y1, (int) $ x1L, (int) $ y1L, $ g );
- ImageLine ($ im, (int) $ x1, (int) $ y1, (int) $ x1R, (int) $ y1R, $ g );
-
- // Re-recursive call itself
- DrawLeaf ($ g, $ x2, $ y2, $ L/$ s3, $ a + $ C );
- DrawLeaf ($ g, $ x2R, $ y2R, $ L/$ s2, $ a + $ B );
- DrawLeaf ($ g, $ x2L, $ y2L, $ L/$ s2, $ a-$ B );
- DrawLeaf ($ g, $ x1L, $ y1L, $ L/$ s2, $ a-$ B );
- DrawLeaf ($ g, $ x1R, $ y1R, $ L/$ s2, $ a + $ B );
- }
- }
-
- // Instantiate
- DrawLeaf ($ g, 300,500,100,270 );
- Header ("Content-type: image/png ");
- Imagepng ($ im );
- ?>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.
-
- Header ("Content-type: text/plain ");
- Functionstatic_function (){
- Static $I=0;
- If ($ I ++<10){
- Echo $ I. "n ";
- Static_function ();
- }
- }
- 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...