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>
关于这些会在之后的垃圾回收里详细提及。

여전히 인기있는 것은 사용 편의성, 유연성 및 강력한 생태계입니다. 1) 사용 편의성과 간단한 구문은 초보자에게 첫 번째 선택입니다. 2) 웹 개발, HTTP 요청 및 데이터베이스와의 우수한 상호 작용과 밀접하게 통합되었습니다. 3) 거대한 생태계는 풍부한 도구와 라이브러리를 제공합니다. 4) 활성 커뮤니티와 오픈 소스 자연은 새로운 요구와 기술 동향에 맞게 조정됩니다.

PHP와 Python은 웹 개발, 데이터 처리 및 자동화 작업에 널리 사용되는 고급 프로그래밍 언어입니다. 1.PHP는 종종 동적 웹 사이트 및 컨텐츠 관리 시스템을 구축하는 데 사용되며 Python은 종종 웹 프레임 워크 및 데이터 과학을 구축하는 데 사용됩니다. 2.PHP는 Echo를 사용하여 콘텐츠를 출력하고 Python은 인쇄를 사용합니다. 3. 객체 지향 프로그래밍을 지원하지만 구문과 키워드는 다릅니다. 4. PHP는 약한 유형 변환을 지원하는 반면, 파이썬은 더 엄격합니다. 5. PHP 성능 최적화에는 Opcache 및 비동기 프로그래밍 사용이 포함되며 Python은 Cprofile 및 비동기 프로그래밍을 사용합니다.

PHP는 주로 절차 적 프로그래밍이지만 객체 지향 프로그래밍 (OOP)도 지원합니다. Python은 OOP, 기능 및 절차 프로그래밍을 포함한 다양한 패러다임을 지원합니다. PHP는 웹 개발에 적합하며 Python은 데이터 분석 및 기계 학습과 같은 다양한 응용 프로그램에 적합합니다.

PHP는 1994 년에 시작되었으며 Rasmuslerdorf에 의해 개발되었습니다. 원래 웹 사이트 방문자를 추적하는 데 사용되었으며 점차 서버 측 스크립팅 언어로 진화했으며 웹 개발에 널리 사용되었습니다. Python은 1980 년대 후반 Guidovan Rossum에 의해 개발되었으며 1991 년에 처음 출시되었습니다. 코드 가독성과 단순성을 강조하며 과학 컴퓨팅, 데이터 분석 및 기타 분야에 적합합니다.

PHP는 웹 개발 및 빠른 프로토 타이핑에 적합하며 Python은 데이터 과학 및 기계 학습에 적합합니다. 1.PHP는 간단한 구문과 함께 동적 웹 개발에 사용되며 빠른 개발에 적합합니다. 2. Python은 간결한 구문을 가지고 있으며 여러 분야에 적합하며 강력한 라이브러리 생태계가 있습니다.

PHP는 현대화 프로세스에서 많은 웹 사이트 및 응용 프로그램을 지원하고 프레임 워크를 통해 개발 요구에 적응하기 때문에 여전히 중요합니다. 1.PHP7은 성능을 향상시키고 새로운 기능을 소개합니다. 2. Laravel, Symfony 및 Codeigniter와 같은 현대 프레임 워크는 개발을 단순화하고 코드 품질을 향상시킵니다. 3. 성능 최적화 및 모범 사례는 응용 프로그램 효율성을 더욱 향상시킵니다.

phphassignificallyimpactedwebdevelopmentandextendsbeyondit

PHP 유형은 코드 품질과 가독성을 향상시키기위한 프롬프트입니다. 1) 스칼라 유형 팁 : PHP7.0이므로 int, float 등과 같은 기능 매개 변수에 기본 데이터 유형을 지정할 수 있습니다. 2) 반환 유형 프롬프트 : 기능 반환 값 유형의 일관성을 확인하십시오. 3) Union 유형 프롬프트 : PHP8.0이므로 기능 매개 변수 또는 반환 값에 여러 유형을 지정할 수 있습니다. 4) Nullable 유형 프롬프트 : NULL 값을 포함하고 널 값을 반환 할 수있는 기능을 포함 할 수 있습니다.


핫 AI 도구

Undresser.AI Undress
사실적인 누드 사진을 만들기 위한 AI 기반 앱

AI Clothes Remover
사진에서 옷을 제거하는 온라인 AI 도구입니다.

Undress AI Tool
무료로 이미지를 벗다

Clothoff.io
AI 옷 제거제

AI Hentai Generator
AI Hentai를 무료로 생성하십시오.

인기 기사

뜨거운 도구

PhpStorm 맥 버전
최신(2018.2.1) 전문 PHP 통합 개발 도구

WebStorm Mac 버전
유용한 JavaScript 개발 도구

SublimeText3 Mac 버전
신 수준의 코드 편집 소프트웨어(SublimeText3)

SublimeText3 Linux 새 버전
SublimeText3 Linux 최신 버전

Eclipse용 SAP NetWeaver 서버 어댑터
Eclipse를 SAP NetWeaver 애플리케이션 서버와 통합합니다.
