Home Backend Development PHP Tutorial Describe PHP recursive algorithm_PHP tutorial

Describe PHP recursive algorithm_PHP tutorial

Jul 15, 2016 pm 01:27 PM
php one time take Compare use Research algorithm still recursion

PHP is still relatively commonly used, so I studied the PHP recursive algorithm and shared it with you here. I hope it will be useful to everyone. PHP, a nested abbreviation name, is the abbreviation of English Hypertext Preprocessing Language (PHP: Hypertext Preprocessor). PHP is an HTML embedded language. It is a scripting language that is executed on the server side and embedded in HTML documents. The style of the language is similar to C language. It is now widely used by many website programmers. PHP's unique syntax is a mix of C, Java, Perl, and PHP's own innovative syntax.

It can execute dynamic web pages faster than CGI or Perl. Compared with other programming languages, dynamic pages made with PHP embed the program into the HTML document for execution, and the execution efficiency is much higher than CGI that completely generates HTML tags; compared with the scripting language JavaScript, which is also embedded in the HTML document In contrast, PHP is executed on the server side, making full use of the server's performance; the PHP execution engine will also store PHP programs that users frequently access in memory, so other users do not need to recompile the program when they access the program again. Just execute the code in the memory directly, which is also one of the manifestations of PHP's high efficiency.

PHP has very powerful functions. PHP can implement all CGI or JavaScript functions and supports almost all popular databases and operating systems. Here we introduce the PHP recursive algorithm in detail.

PHP recursive algorithm code:

<ol class="dp-xml">
<li class="alt"><span><span class="tag"><?</SPAN><SPAN class=tag-name>php</SPAN><SPAN> </SPAN></SPAN><LI class=""><SPAN>//定义PI一分的角度的值  </SPAN><LI class=alt><SPAN>define("PII",M_PI/180);  </SPAN><LI class=""><SPAN> </SPAN><LI class=alt><SPAN>//新建图像资源,并定义其背景为白色,前景色为黑色  </SPAN><LI class=""><SPAN>$</SPAN><SPAN class=attribute>im</SPAN><SPAN>=</SPAN><SPAN class=attribute-value>imagecreate</SPAN><SPAN>(670,500);  </SPAN></SPAN><LI class=alt><SPAN>$</SPAN><SPAN class=attribute>white</SPAN><SPAN>=</SPAN><SPAN class=attribute-value>imagecolorallocate</SPAN><SPAN>($im,0xFF,0xFF,0xFF);  </SPAN></SPAN><LI class=""><SPAN>$</SPAN><SPAN class=attribute>g</SPAN><SPAN>=</SPAN><SPAN class=attribute-value>imagecolorallocate</SPAN><SPAN>($im,0x00,0x00,0x00);  </SPAN></SPAN><LI class=alt><SPAN> </SPAN><LI class=""><SPAN>//从下面实例化的代码可以得知,初始值$x,$y,$L,$a别分为300,500,100,270  </SPAN><LI class=alt><SPAN>functiondrawLeaf($g,$x,$y,$L,$a){  </SPAN><LI class=""><SPAN>global$im;  </SPAN><LI class=alt><SPAN>$</SPAN><SPAN class=attribute>B</SPAN><SPAN>=</SPAN><SPAN class=attribute-value>50</SPAN><SPAN>;  </SPAN></SPAN><LI class=""><SPAN>$</SPAN><SPAN class=attribute>C</SPAN><SPAN>=</SPAN><SPAN class=attribute-value>9</SPAN><SPAN>;  </SPAN></SPAN><LI class=alt><SPAN>$</SPAN><SPAN class=attribute>s1</SPAN><SPAN>=</SPAN><SPAN class=attribute-value>2</SPAN><SPAN>;  </SPAN></SPAN><LI class=""><SPAN>$</SPAN><SPAN class=attribute>s2</SPAN><SPAN>=</SPAN><SPAN class=attribute-value>3</SPAN><SPAN>;  </SPAN></SPAN><LI class=alt><SPAN>$</SPAN><SPAN class=attribute>s3</SPAN><SPAN>=</SPAN><SPAN class=attribute-value>1</SPAN><SPAN>.2;  </SPAN></SPAN><LI class=""><SPAN>if($L</SPAN><SPAN class=tag>></span><span>$s1){  </span></span></li>
<li class="alt"><span>//计算叶子的定位上面  </span></li>
<li class="">
<span>$</span><span class="attribute">x2</span><span>=$x+$L*cos($a*PII);  </span>
</li>
<li class="alt">
<span>$</span><span class="attribute">y2</span><span>=$y+$L*sin($a*PII);  </span>
</li>
<li class="">
<span>$</span><span class="attribute">x2R</span><span>=$x2+$L/$s2*cos(($a+$B)*PII);  </span>
</li>
<li class="alt">
<span>$</span><span class="attribute">y2R</span><span>=$y2+$L/$s2*sin(($a+$B)*PII);  </span>
</li>
<li class="">
<span>$</span><span class="attribute">x2L</span><span>=$x2+$L/$s2*cos(($a-$B)*PII);  </span>
</li>
<li class="alt">
<span>$</span><span class="attribute">y2L</span><span>=$y2+$L/$s2*sin(($a-$B)*PII);  </span>
</li>
<li class=""><span> </span></li>
<li class="alt"><span>//计算叶子的定位下面  </span></li>
<li class="">
<span>$</span><span class="attribute">x1</span><span>=$x+$L/$s2*cos($a*PII);  </span>
</li>
<li class="alt">
<span>$</span><span class="attribute">y1</span><span>=$y+$L/$s2*sin($a*PII);  </span>
</li>
<li class="">
<span>$</span><span class="attribute">x1L</span><span>=$x1+$L/$s2*cos(($a-$B)*PII);  </span>
</li>
<li class="alt">
<span>$</span><span class="attribute">y1L</span><span>=$y1+$L/$s2*sin(($a-$B)*PII);  </span>
</li>
<li class="">
<span>$</span><span class="attribute">x1R</span><span>=$x1+$L/$s2*cos(($a+$B)*PII);  </span>
</li>
<li class="alt">
<span>$</span><span class="attribute">y1R</span><span>=$y1+$L/$s2*sin(($a+$B)*PII);  </span>
</li>
<li class=""><span> </span></li>
<li class="alt"><span>//别分画叶子的主干以及叶面  </span></li>
<li class=""><span>ImageLine($im,(int)$x,(int)$y,(int)$x2,(int)$y2,$g);  </span></li>
<li class="alt"><span>ImageLine($im,(int)$x2,(int)$y2,(int)$x2R,(int)$y2R,$g);  </span></li>
<li class=""><span>ImageLine($im,(int)$x2,(int)$y2,(int)$x2L,(int)$y2L,$g);  </span></li>
<li class="alt"><span>ImageLine($im,(int)$x1,(int)$y1,(int)$x1L,(int)$y1L,$g);  </span></li>
<li class=""><span>ImageLine($im,(int)$x1,(int)$y1,(int)$x1R,(int)$y1R,$g);  </span></li>
<li class="alt"><span> </span></li>
<li class=""><span>//再次递归调用本身  </span></li>
<li class="alt"><span>drawLeaf($g,$x2,$y2,$L/$s3,$a+$C);  </span></li>
<li class=""><span>drawLeaf($g,$x2R,$y2R,$L/$s2,$a+$B);  </span></li>
<li class="alt"><span>drawLeaf($g,$x2L,$y2L,$L/$s2,$a-$B);  </span></li>
<li class=""><span>drawLeaf($g,$x1L,$y1L,$L/$s2,$a-$B);  </span></li>
<li class="alt"><span>drawLeaf($g,$x1R,$y1R,$L/$s2,$a+$B);  </span></li>
<li class=""><span>}  </span></li>
<li class="alt"><span>}  </span></li>
<li class=""><span> </span></li>
<li class="alt"><span>//实例化  </span></li>
<li class=""><span>drawLeaf($g,300,500,100,270);  </span></li>
<li class="alt"><span>header("Content-type:image/png");  </span></li>
<li class=""><span>imagepng($im);  </span></li>
<li class="alt">
<span></span><span class="tag">?></span><span>在我个人的PHP编程经验中,递归调用常常与静态变量使用。静态变量的含义可以参考PHP手册。希望下面的代码,会更有利于对递归以及静态变量的理解  </span>
</li>
<li class=""><span> </span></li>
<li class="alt"><span>header("Content-type:text/plain");  </span></li>
<li class=""><span>functionstatic_function(){  </span></li>
<li class="alt">
<span>static$</span><span class="attribute">i</span><span>=</span><span class="attribute-value">0</span><span>;  </span>
</li>
<li class="">
<span>if($i++</span><span class="tag"><</span><span class="tag-name">10</span><span>){  </span>
</li>
<li class="alt"><span>echo$i."n";  </span></li>
<li class=""><span>static_function();  </span></li>
<li class="alt"><span>}  </span></li>
<li class=""><span>}  </span></li>
<li class="alt"><span>static_function(); </span></li>
</ol>
Copy after login

This code will output numbers from 1 to 10. When the static_function function runs for the second time, the variable i is still retained and not released because it is a static variable, and the auto-incremented value can be obtained.


www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/446491.htmlTechArticlePHP is still relatively commonly used, so I studied the PHP recursive algorithm and share it with you here. Hope it's useful to everyone. PHP, a nested abbreviation name, is the English super...
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
Two Point Museum: All Exhibits And Where To Find Them
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

CakePHP Project Configuration CakePHP Project Configuration Sep 10, 2024 pm 05:25 PM

In this chapter, we will understand the Environment Variables, General Configuration, Database Configuration and Email Configuration in CakePHP.

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

CakePHP Date and Time CakePHP Date and Time Sep 10, 2024 pm 05:27 PM

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

CakePHP File upload CakePHP File upload Sep 10, 2024 pm 05:27 PM

To work on file upload we are going to use the form helper. Here, is an example for file upload.

CakePHP Routing CakePHP Routing Sep 10, 2024 pm 05:25 PM

In this chapter, we are going to learn the following topics related to routing ?

Discuss CakePHP Discuss CakePHP Sep 10, 2024 pm 05:28 PM

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

CakePHP Creating Validators CakePHP Creating Validators Sep 10, 2024 pm 05:26 PM

Validator can be created by adding the following two lines in the controller.

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

See all articles