一步一步学习PHP(3) php 函数_PHP教程
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 dan Python masing -masing mempunyai kelebihan sendiri, dan pilihannya harus berdasarkan keperluan projek. 1.Php sesuai untuk pembangunan web, dengan sintaks mudah dan kecekapan pelaksanaan yang tinggi. 2. Python sesuai untuk sains data dan pembelajaran mesin, dengan sintaks ringkas dan perpustakaan yang kaya.

PHP tidak mati, tetapi sentiasa menyesuaikan diri dan berkembang. 1) PHP telah menjalani beberapa lelaran versi sejak tahun 1994 untuk menyesuaikan diri dengan trend teknologi baru. 2) Ia kini digunakan secara meluas dalam e-dagang, sistem pengurusan kandungan dan bidang lain. 3) Php8 memperkenalkan pengkompil JIT dan fungsi lain untuk meningkatkan prestasi dan pemodenan. 4) Gunakan OPCACHE dan ikut piawaian PSR-12 untuk mengoptimumkan prestasi dan kualiti kod.

Masa depan PHP akan dicapai dengan menyesuaikan diri dengan trend teknologi baru dan memperkenalkan ciri -ciri inovatif: 1) menyesuaikan diri dengan pengkomputeran awan, kontena dan seni bina microservice, menyokong Docker dan Kubernetes; 2) memperkenalkan pengkompil JIT dan jenis penghitungan untuk meningkatkan prestasi dan kecekapan pemprosesan data; 3) Berterusan mengoptimumkan prestasi dan mempromosikan amalan terbaik.

Dalam PHP, sifat sesuai untuk situasi di mana penggunaan semula kaedah diperlukan tetapi tidak sesuai untuk warisan. 1) Ciri membolehkan kaedah multiplexing dalam kelas untuk mengelakkan pelbagai kerumitan warisan. 2) Apabila menggunakan sifat, anda perlu memberi perhatian kepada konflik kaedah, yang dapat diselesaikan melalui alternatif dan sebagai kata kunci. 3) Tua yang berlebihan harus dielakkan dan tanggungjawab tunggalnya harus dikekalkan untuk mengoptimumkan prestasi dan meningkatkan pemeliharaan kod.

Kontena Suntikan Ketergantungan (DIC) adalah alat yang menguruskan dan menyediakan kebergantungan objek untuk digunakan dalam projek PHP. Manfaat utama DIC termasuk: 1. Decoupling, membuat komponen bebas, dan kod itu mudah dikekalkan dan diuji; 2. Fleksibiliti, mudah untuk menggantikan atau mengubah suai kebergantungan; 3. Keseluruhan, mudah untuk menyuntik objek mengejek untuk ujian unit.

SplfixedArray adalah pelbagai saiz tetap dalam PHP, sesuai untuk senario di mana prestasi tinggi dan penggunaan memori yang rendah diperlukan. 1) Ia perlu menentukan saiz apabila membuat untuk mengelakkan overhead yang disebabkan oleh pelarasan dinamik. 2) Berdasarkan pelbagai bahasa C, secara langsung mengendalikan memori dan kelajuan akses cepat. 3) Sesuai untuk pemprosesan data berskala besar dan persekitaran sensitif memori, tetapi ia perlu digunakan dengan berhati-hati kerana saiznya tetap.

PHP mengendalikan fail muat naik melalui pembolehubah fail $ \ _. Kaedah untuk memastikan keselamatan termasuk: 1. Semak kesilapan muat naik, 2. Sahkan jenis dan saiz fail, 3. Mencegah penindasan fail, 4. Pindahkan fail ke lokasi storan tetap.

Dalam JavaScript, anda boleh menggunakan NullcoalescingOperator (??) dan NullcoalescingAssignmentOperator (?? =). 1.? Menerapkan semula operan pertama yang tidak berselisih atau tidak ditentukan. 2.?? Pengendali ini memudahkan logik kod, meningkatkan kebolehbacaan dan prestasi.


Alat AI Hot

Undresser.AI Undress
Apl berkuasa AI untuk mencipta foto bogel yang realistik

AI Clothes Remover
Alat AI dalam talian untuk mengeluarkan pakaian daripada foto.

Undress AI Tool
Gambar buka pakaian secara percuma

Clothoff.io
Penyingkiran pakaian AI

AI Hentai Generator
Menjana ai hentai secara percuma.

Artikel Panas

Alat panas

ZendStudio 13.5.1 Mac
Persekitaran pembangunan bersepadu PHP yang berkuasa

Muat turun versi mac editor Atom
Editor sumber terbuka yang paling popular

Pelayar Peperiksaan Selamat
Pelayar Peperiksaan Selamat ialah persekitaran pelayar selamat untuk mengambil peperiksaan dalam talian dengan selamat. Perisian ini menukar mana-mana komputer menjadi stesen kerja yang selamat. Ia mengawal akses kepada mana-mana utiliti dan menghalang pelajar daripada menggunakan sumber yang tidak dibenarkan.

SublimeText3 Linux versi baharu
SublimeText3 Linux versi terkini

SublimeText3 versi Cina
Versi Cina, sangat mudah digunakan