1. 方法概述
首先,写一个最简单的函数,大家看一眼就可以了:
<SPAN style="COLOR: blue"><</SPAN><SPAN style="COLOR: maroon">html</SPAN><SPAN style="COLOR: blue">> <</SPAN><SPAN style="COLOR: maroon">head</SPAN><SPAN style="COLOR: blue">> <</SPAN><SPAN style="COLOR: maroon">title</SPAN><SPAN style="COLOR: blue">></SPAN>HelloPHP<SPAN style="COLOR: blue"></</SPAN><SPAN style="COLOR: maroon">title</SPAN><SPAN style="COLOR: blue">> </</SPAN><SPAN style="COLOR: maroon">head</SPAN><SPAN style="COLOR: blue">> <</SPAN><SPAN style="COLOR: maroon">body</SPAN><SPAN style="COLOR: blue">> <?</SPAN><SPAN style="COLOR: maroon">php </SPAN><SPAN style="COLOR: red">function CustomPrint</SPAN>($<SPAN style="COLOR: red">str</SPAN>) { <SPAN style="COLOR: red">for</SPAN>($<SPAN style="COLOR: red">i</SPAN><SPAN style="COLOR: blue">=0;$i</SPAN><5;$<SPAN style="COLOR: red">i</SPAN>++) { <SPAN style="COLOR: red">echo</SPAN>($<SPAN style="COLOR: red">str</SPAN>); <SPAN style="COLOR: red">echo</SPAN>(<SPAN style="COLOR: blue">'<br/>'</SPAN>); } } <SPAN style="COLOR: red">CustomPrint</SPAN>(<SPAN style="COLOR: blue">"Hello"</SPAN>); <SPAN style="COLOR: blue">?> </</SPAN><SPAN style="COLOR: maroon">body</SPAN><SPAN style="COLOR: blue">> </</SPAN><SPAN style="COLOR: maroon">html</SPAN><SPAN style="COLOR: blue">> </SPAN>
通过这个例子,相信大家都了解了PHP中函数的大致写法,至于语法上,和其他类C语言差别不大,也都是while,for,if等,至于其他不同之处,会在之后的文章中,逐渐来说。
OK,那我来总结一下,这个方法的要点:
A. PHP的方法用function来声明,这一点类似于我们熟悉的Javascript.
B. 在使用变量必须要以美元符($)来开头。
2. 参数的引用传递和值传递
参数的值传递和引用传递,相信每个人在学习C语言时都接触到了,在此用C#来写例子:
<SPAN style="COLOR: blue">public void </SPAN>Swap(<SPAN style="COLOR: blue">int </SPAN>a, <SPAN style="COLOR: blue">int </SPAN>b) { <SPAN style="COLOR: blue">int </SPAN>temp = a; a = b; b = temp; } <SPAN style="COLOR: blue">public void </SPAN>Swap(<SPAN style="COLOR: blue">ref int </SPAN>a, <SPAN style="COLOR: blue">ref int </SPAN>b) { <SPAN style="COLOR: blue">int </SPAN>temp = a; a = b; b = temp; }
那在这里就写一个PHP版本。
<SPAN style="COLOR: blue"><?</SPAN><SPAN style="COLOR: maroon">php </SPAN><SPAN style="COLOR: red">function Swap1</SPAN>($<SPAN style="COLOR: red">a</SPAN>,$<SPAN style="COLOR: red">b</SPAN>) { $<SPAN style="COLOR: red">temp</SPAN><SPAN style="COLOR: blue">=$a; </SPAN>$<SPAN style="COLOR: red">a</SPAN><SPAN style="COLOR: blue">=$b; </SPAN>$<SPAN style="COLOR: red">b</SPAN><SPAN style="COLOR: blue">=$temp; </SPAN>} <SPAN style="COLOR: red">function Swap2</SPAN>(&$<SPAN style="COLOR: red">a</SPAN>,&$<SPAN style="COLOR: red">b</SPAN>) { $<SPAN style="COLOR: red">temp</SPAN><SPAN style="COLOR: blue">=$a; </SPAN>$<SPAN style="COLOR: red">a</SPAN><SPAN style="COLOR: blue">=$b; </SPAN>$<SPAN style="COLOR: red">b</SPAN><SPAN style="COLOR: blue">=$temp; </SPAN>} <SPAN style="COLOR: red">function CustomPrint</SPAN>($<SPAN style="COLOR: red">str</SPAN>) { <SPAN style="COLOR: red">echo</SPAN>($<SPAN style="COLOR: red">str</SPAN>); <SPAN style="COLOR: red">echo</SPAN>(<SPAN style="COLOR: blue">"<br/>"</SPAN>); } $<SPAN style="COLOR: red">a</SPAN><SPAN style="COLOR: blue">=1; </SPAN>$<SPAN style="COLOR: red">b</SPAN><SPAN style="COLOR: blue">=2; </SPAN><SPAN style="COLOR: red">Swap1</SPAN>($<SPAN style="COLOR: red">a</SPAN>,$<SPAN style="COLOR: red">b</SPAN>); <SPAN style="COLOR: red">CustomPrint</SPAN>(<SPAN style="COLOR: blue">"值传递的结果:"</SPAN>); <SPAN style="COLOR: red">CustomPrint</SPAN>(<SPAN style="COLOR: blue">'$a='</SPAN>.$<SPAN style="COLOR: red">a</SPAN>); <SPAN style="COLOR: red">CustomPrint</SPAN>(<SPAN style="COLOR: blue">'$b='</SPAN>.$<SPAN style="COLOR: red">b</SPAN>); $<SPAN style="COLOR: red">a</SPAN><SPAN style="COLOR: blue">=1; </SPAN>$<SPAN style="COLOR: red">b</SPAN><SPAN style="COLOR: blue">=2; </SPAN><SPAN style="COLOR: red">Swap2</SPAN>($<SPAN style="COLOR: red">a</SPAN>,$<SPAN style="COLOR: red">b</SPAN>); <SPAN style="COLOR: red">CustomPrint</SPAN>(<SPAN style="COLOR: blue">"引用传递的结果:"</SPAN>); <SPAN style="COLOR: red">CustomPrint</SPAN>(<SPAN style="COLOR: blue">'$a='</SPAN>.$<SPAN style="COLOR: red">a</SPAN>); <SPAN style="COLOR: red">CustomPrint</SPAN>(<SPAN style="COLOR: blue">'$b='</SPAN>.$<SPAN style="COLOR: red">b</SPAN>); <SPAN style="COLOR: blue">?> </SPAN>
在这个例子中:有两点我需要说明:
A. 值传递和引用传递的区别在于在参数前的“&”.
B. CustomPrint('$a='.$a);在这句中,需要特殊说明一下单引号和双引号的区别,他们之间只有一个区别,就是能否解析变量名,这个例子就足够说明问题了:
<SPAN style="COLOR: blue"><?</SPAN><SPAN style="COLOR: maroon">php </SPAN>$<SPAN style="COLOR: red">a</SPAN><SPAN style="COLOR: blue">=1; </SPAN><SPAN style="COLOR: red">echo</SPAN>(<SPAN style="COLOR: blue">"$a"</SPAN>); <SPAN style="COLOR: red">echo</SPAN>(<SPAN style="COLOR: blue">"<br/>"</SPAN>); <SPAN style="COLOR: red">echo</SPAN>(<SPAN style="COLOR: blue">'$a'</SPAN>); <SPAN style="COLOR: blue">?> </SPAN>
![]()
最后,说下关于性能的问题,在按值传递时,PHP需要进行复制,然后再传递,这样如果那些大对象或者字符串的话,就会不仅仅耗费时间,而且对空间也是一种浪费。这时,如果进行引用传递,就免去了耗费性能的复制操作。对性能提高很有好处。
3. 作用域问题
在C#中,由于变量在使用之前必须声明,因此会涉及到一个作用域和子作用域的概念,而在PHP中则没有这样的概念。
我们来看一段C#代码:
<SPAN style="COLOR: blue">public class </SPAN><SPAN style="COLOR: #2b91af">Student </SPAN>{ <SPAN style="COLOR: blue">private string </SPAN>name; <SPAN style="COLOR: blue">public void </SPAN>SayHello() { <SPAN style="COLOR: #2b91af">HttpContext</SPAN>.Current.Response.Write(<SPAN style="COLOR: #a31515">"Hello,I am " </SPAN>+ name); } }
也就是说,在方法内可以访问外部类声明的变量,但是在PHP中则不一样:
<SPAN style="COLOR: blue"><?</SPAN><SPAN style="COLOR: maroon">php </SPAN>$<SPAN style="COLOR: red">name</SPAN><SPAN style="COLOR: blue">="kym"</SPAN>; <SPAN style="COLOR: red">function SayHello</SPAN>() { <SPAN style="COLOR: red">if</SPAN>(<SPAN style="COLOR: red">isset</SPAN>($<SPAN style="COLOR: red">name</SPAN>)) { <SPAN style="COLOR: red">echo</SPAN>(<SPAN style="COLOR: blue">"Hello $name"</SPAN>); } <SPAN style="COLOR: red">else </SPAN>{ <SPAN style="COLOR: red">echo</SPAN>(<SPAN style="COLOR: blue">'$name is undefined'</SPAN>); } } <SPAN style="COLOR: red">SayHello</SPAN>(); <SPAN style="COLOR: blue">?> </SPAN>
这里说明一个函数“isset”,这个函数可以检测一个变量是否被定义,或者是否是空字符串。
那么这个结果说明,在函数体内,无法访问到外部变量$name。
在这里在多提一点:一个与unset对应的函数:unset。该函数用于移除一个变量的值。
写个简单的例子:
<SPAN style="COLOR: red"><?php </SPAN><SPAN style="COLOR: #660000">$name</SPAN>=<SPAN style="COLOR: #008200">"kym"</SPAN>; <SPAN style="COLOR: blue">if</SPAN>(<SPAN style="COLOR: blue">isset</SPAN>(<SPAN style="COLOR: #660000">$name</SPAN>)) { <SPAN style="COLOR: blue">echo</SPAN>(<SPAN style="COLOR: #008200">"Yes"</SPAN>); } <SPAN style="COLOR: blue">else </SPAN>{ <SPAN style="COLOR: blue">echo</SPAN>(<SPAN style="COLOR: #008200">"No"</SPAN>); } <SPAN style="COLOR: blue">unset</SPAN>(<SPAN style="COLOR: #660000">$name</SPAN>); <SPAN style="COLOR: blue">if</SPAN>(<SPAN style="COLOR: blue">isset</SPAN>(<SPAN style="COLOR: #660000">$name</SPAN>)) { <SPAN style="COLOR: blue">echo</SPAN>(<SPAN style="COLOR: #008200">"Yes"</SPAN>); } <SPAN style="COLOR: blue">else </SPAN>{ <SPAN style="COLOR: blue">echo</SPAN>(<SPAN style="COLOR: #008200">"No"</SPAN>); } <SPAN style="COLOR: red">?></SPAN>
关于这些会在之后的垃圾回收里详细提及。

PHP ist hauptsächlich prozedurale Programmierung, unterstützt aber auch die objektorientierte Programmierung (OOP). Python unterstützt eine Vielzahl von Paradigmen, einschließlich OOP, funktionaler und prozeduraler Programmierung. PHP ist für die Webentwicklung geeignet, und Python eignet sich für eine Vielzahl von Anwendungen wie Datenanalyse und maschinelles Lernen.

PHP entstand 1994 und wurde von Rasmuslerdorf entwickelt. Es wurde ursprünglich verwendet, um Website-Besucher zu verfolgen und sich nach und nach zu einer serverseitigen Skriptsprache entwickelt und in der Webentwicklung häufig verwendet. Python wurde Ende der 1980er Jahre von Guidovan Rossum entwickelt und erstmals 1991 veröffentlicht. Es betont die Lesbarkeit und Einfachheit der Code und ist für wissenschaftliche Computer, Datenanalysen und andere Bereiche geeignet.

PHP eignet sich für Webentwicklung und schnelles Prototyping, und Python eignet sich für Datenwissenschaft und maschinelles Lernen. 1.PHP wird für die dynamische Webentwicklung verwendet, mit einfacher Syntax und für schnelle Entwicklung geeignet. 2. Python hat eine kurze Syntax, ist für mehrere Felder geeignet und ein starkes Bibliotheksökosystem.

PHP bleibt im Modernisierungsprozess wichtig, da es eine große Anzahl von Websites und Anwendungen unterstützt und sich den Entwicklungsbedürfnissen durch Frameworks anpasst. 1.PHP7 verbessert die Leistung und führt neue Funktionen ein. 2. Moderne Frameworks wie Laravel, Symfony und Codesigniter vereinfachen die Entwicklung und verbessern die Codequalität. 3.. Leistungsoptimierung und Best Practices verbessern die Anwendungseffizienz weiter.

PhPhas significantantyPactedWebDevelopmentAndendendsbeyondit.1) iTpowersMAjorPlatforms-LikewordpressandExcelsInDatabaseInteractions.2) php'SadaptabilityAllowStoscaleForLargeApplicationsfraMe-Linien-Linien-Linien-Linienkripte

PHP -Typ -Eingabeaufforderungen zur Verbesserung der Codequalität und der Lesbarkeit. 1) Tipps zum Skalartyp: Da Php7.0 in den Funktionsparametern wie int, float usw. angegeben werden dürfen. 3) Eingabeaufforderung für Gewerkschaftstyp: Da Php8.0 in Funktionsparametern oder Rückgabetypen angegeben werden dürfen. 4) Nullierstyp Eingabeaufforderung: Ermöglicht die Einbeziehung von Nullwerten und Handlungsfunktionen, die Nullwerte zurückgeben können.

Verwenden Sie in PHP das Klonschlüsselwort, um eine Kopie des Objekts zu erstellen und das Klonierungsverhalten über die \ _ \ _ Clone Magic -Methode anzupassen. 1. Verwenden Sie das Klonschlüsselwort, um eine flache Kopie zu erstellen und die Eigenschaften des Objekts, nicht die Eigenschaften des Objekts zu klonen. 2. Die \ _ \ _ Klonmethode kann verschachtelte Objekte tief kopieren, um flache Kopierprobleme zu vermeiden. 3. achten Sie darauf, dass kreisförmige Referenzen und Leistungsprobleme beim Klonen vermieden werden, und optimieren Sie die Klonierungsvorgänge, um die Effizienz zu verbessern.

PHP eignet sich für Webentwicklungs- und Content -Management -Systeme, und Python eignet sich für Datenwissenschafts-, maschinelles Lernen- und Automatisierungsskripte. 1.PHP hat eine gute Leistung beim Erstellen von schnellen und skalierbaren Websites und Anwendungen und wird üblicherweise in CMS wie WordPress verwendet. 2. Python hat sich in den Bereichen Datenwissenschaft und maschinelles Lernen mit reichen Bibliotheken wie Numpy und TensorFlow übertrifft.


Heiße KI -Werkzeuge

Undresser.AI Undress
KI-gestützte App zum Erstellen realistischer Aktfotos

AI Clothes Remover
Online-KI-Tool zum Entfernen von Kleidung aus Fotos.

Undress AI Tool
Ausziehbilder kostenlos

Clothoff.io
KI-Kleiderentferner

AI Hentai Generator
Erstellen Sie kostenlos Ai Hentai.

Heißer Artikel

Heiße Werkzeuge

PHPStorm Mac-Version
Das neueste (2018.2.1) professionelle, integrierte PHP-Entwicklungstool

SAP NetWeaver Server-Adapter für Eclipse
Integrieren Sie Eclipse mit dem SAP NetWeaver-Anwendungsserver.

SublimeText3 Englische Version
Empfohlen: Win-Version, unterstützt Code-Eingabeaufforderungen!

Herunterladen der Mac-Version des Atom-Editors
Der beliebteste Open-Source-Editor

Dreamweaver Mac
Visuelle Webentwicklungstools