Learn PHP(3) php functions step by step_PHP tutorial
1. Method Overview
First, write the simplest function, which everyone can take a look at:
<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>
Through this example, I believe everyone understands the general writing method of functions in PHP. As for the syntax, it is not much different from other C-like languages. They are also while, for, if, etc. As for other differences , will be discussed gradually in subsequent articles.
OK, let me summarize the key points of this method:
A. PHP methods are declared with functions, which is similar to the familiar Javascript.
B. When using variables, they must start with a dollar sign ($).
2. Reference passing and value passing of parameters
Parameter value passing and reference passing, I believe everyone has come into contact with it when learning C language. Here is an example using 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; }
Then write a PHP version here.
<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>
In this example: There are two points I need to make clear:
A. The difference between value passing and reference passing is the "&" before the parameter.
B. CustomPrint('$a='.$a); In this sentence, special explanation is needed about the single quotes and The only difference between double quotes is whether the variable name can be parsed. This example is enough to illustrate the problem:
<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>Copy after login
![]()
Finally, let’s talk about performance issues. When passing by value, PHP needs to copy and then pass it. In this way, if those large objects or strings are used, it will not only consume time, but also consume space. It's also a waste. At this time, if you pass by reference, you will avoid the performance-consuming copy operation. Very good for performance improvement.
3. Scope issue
In C#, since variables must be declared before being used, the concepts of a scope and subscope are involved, but there is no such concept in PHP.
Let’s take a look at a piece of C# code:
<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); } }
In other words, variables declared by the external class can be accessed within the method, but it is different in 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>
Here is a function "isset", which can detect whether a variable is defined or whether it is an empty string.
Then this result shows that the external variable $name cannot be accessed within the function body.
I would like to mention one more thing here: a function corresponding to unset: unset. This function is used to remove the value of a variable.
Write a simple example:
<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>Copy after login
These will be mentioned in detail in the subsequent garbage collection.

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



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

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

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

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

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

Logging in CakePHP is a very easy task. You just have to use one function. You can log errors, exceptions, user activities, action taken by users, for any background process like cronjob. Logging data in CakePHP is easy. The log() function is provide

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

CakePHP is an open source MVC framework. It makes developing, deploying and maintaining applications much easier. CakePHP has a number of libraries to reduce the overload of most common tasks.
