A detailed example analysis of PHP recursive algorithm _php tips

Source: Internet
Author: User
Tags cos php language php programming sin

When we were building a website, programmers preferred the PHP language. We are more familiar with PHP, and then we will introduce you to the PHP recursive algorithm. PHP, a nested abbreviated name, is an abbreviation for the English Hypertext Preprocessing language (Php:hypertext preprocessor).

PHP is an HTML embedded language, is a server-side embedded HTML document script language, language style has similar to C language, is now widely used by many web programmers. PHP's unique syntax mixes C, Java, Perl, and PHP with its own innovative syntax.
It can execute dynamic Web pages faster than CGI or Perl. Dynamic pages made with PHP compared to other programming languages, PHP embeds programs into HTML documents and executes more efficiently than CGI that completely generates HTML tags; php executes on the server side compared to scripting language JavaScript, which is also embedded in HTML documents. Take full advantage of the performance of the server; the PHP execution engine also hosts the PHP programs that users frequently visit in memory, and other users don't need to recompile the program once again, as long as they execute the code directly in memory, which is one of the most efficient embodiments of PHP.

PHP has a very powerful function, all of the CGI or JavaScript features PHP can be implemented, but also support almost all popular databases and operating systems. Here is a detailed introduction to the PHP recursive algorithm.

PHP Recursive algorithm code:

Copy Code code as follows:

< PHP
Defines the value of a PI-point angle ("PII", m_pi/180); define
Creates a new image resource and defines its background as white, with a foreground color of black
$im =imagecreate (670,500);
$white =imagecolorallocate ($im, 0xff,0xff,0xff);
$g =imagecolorallocate ($im, 0x00,0x00,0x00);
The code instantiated below can be learned that the initial value is $x, $y, $L, $a is divided into 300,500,100,270
Functiondrawleaf ($g, $x, $y, $L, $a)
{Global$im $B =50; $C =9; $s 1=2; $s 2=3; $s 3=1.2;
if ($L > $s 1) {
Calculate the location of the leaves above
$x 2= $x + $L *cos ($a *pii);
$y 2= $y + $L *sin ($a *pii);
$x 2r= $x $L/$s 2*cos (($a + $B) *pii);
$y 2r= $y $L/$s 2*sin (($a + $B) *pii);
$x 2l= $x $L/$s 2*cos (($a-$B) *pii);
$y 2l= $y $L/$s 2*sin (($a-$B) *pii);
Calculate the location of the leaves below
$x 1= $x + $L/$s 2*cos ($a *pii);
$y 1= $y + $L/$s 2*sin ($a *pii);
$x 1l= $x 1+ $L/$s 2*cos (($a-$B) *pii);
$y 1l= $y 1+ $L/$s 2*sin (($a-$B) *pii);
$x 1r= $x 1+ $L/$s 2*cos (($a + $B) *pii);
$y 1r= $y 1+ $L/$s 2*sin (($a + $B) *pii);
Don't divide the trunk and foliage of the leaves.
Imageline ($im, (int) $x, (int) $y, (int) $x 2, (int) $y 2, $g);
Imageline ($im, (int) $x 2, (int) $y 2, (int) $x 2R, (int) $y 2R, $g);
Imageline ($im, (int) $x 2, (int) $y 2, (int) $x 2L, (int) $y 2L, $g);
Imageline ($im, (int) $x 1, (int) $y 1, (int) $x 1L, (int) $y 1L, $g);
Imageline ($im, (int) $x 1, (int) $y 1, (int) $x 1R, (int) $y 1R, $g);
Recursively call itself again
Drawleaf ($g, $x 2, $y 2, $L/$s 3, $a + $C);
Drawleaf ($g, $x 2R, $y 2R, $L/$s 2, $a + $B);
Drawleaf ($g, $x 2L, $y 2L, $L/$s 2, $a-$B);
Drawleaf ($g, $x 1L, $y 1L, $L/$s 2, $a-$B);
Drawleaf ($g, $x 1R, $y 1R, $L/$s 2, $a + $B);
}
}
Instantiation of
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. The meaning of static variables can refer to the PHP manual. Hopefully, the following code will be more useful for understanding PHP recursive algorithms and static variables.
Copy Code code 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 will output 1 to 10 of the number. 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 value of the increment 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.