Detailed analysis of PHP Recursive Algorithms

Source: Internet
Author: User
Tags value of pi

When building a website, programmers prefer the PHP language. We are still familiar with PHP. Next we will introduce the PHP recursive algorithm. 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:
Copy codeThe Code is as follows:
<? Php
// Define the angle value of PI ("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 PHP Recursive Algorithms and static variables.
Copy codeThe Code is as follows:
Header ("Content-type: text/plain"); functionstatic_function ()
{
Static $ I = 0;
If ($ I ++ <10)
{
Echo $ I. "\ n ";
Static_function ();
}
}
Static_function ();

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