Php 简明教程
PHP - Functions
与大多数编程语言类似,PHP 中的一个函数是一段有组织的可重用代码,用于执行一个相关的单一操作。函数为您的应用程序提供了更好的模块化和高程度的代码重用。
Like most of the programming languages, a function in PHP is a block of organized, reusable code that is used to perform a single, related action. Functions provide better modularity for your application and a high degree of code reuse.
PHP 通过通过定义独立的可重用函数块来安排处理逻辑,支持结构化编程方法。这种方法的主要优点是代码变得易于理解、开发和维护。
PHP supports a structured programming approach by arranging the processing logic by defining blocks of independent reusable functions. The main advantage of this approach is that the code becomes easy to follow, develop and maintain.
下图显示了如何将工资计算过程逐步细化为独立的可重用函数。
The following figure shows how the process of salary computation is successively broken down to independent and reusable functions.
Types of Functions
你已经见过了很多诸如 fopen() 和 fread() 等函数。它们是内置函数,但 PHP 也允许你创建你自己的函数。PHP 中有两种类型的函数−
You already have seen many functions like fopen() and fread() etc. They are built-in functions but PHP gives you option to create your own functions as well. There are two types of functions in PHP −
-
Built-in functions − PHP’s standard library contains a large number of built-in functions for string processing, file IO, mathematical computations and more.
-
User-defined functions − You can create user-defined functions too, specific to the requirements of the programming logic.
可以通过传递必要的数据(称为 parameters 或 arguments ),让一个函数从任何其他函数调用。被调用的函数将它的结果返回给调用环境。
A function may be invoked from any other function by passing required data (called parameters or arguments). The called function returns its result back to the calling environment.
这里有两部分需要明确 −
There are two parts which should be clear to you −
-
Creating a PHP Function
-
Calling a PHP Function
事实上,你几乎不需要创建你自己的 PHP 函数,因为已经为不同的区域创建了超过 1000 个内置库函数,你只需要根据你的需求调用它们。
In fact you hardly need to create your own PHP function because there are already more than 1000 built-in library functions created for different area and you just need to call them according to your requirement.
请参阅 PHP Function Reference ,了解一组有用的函数。
Please refer to PHP Function Reference for a complete set of useful functions.
User-defined Functions in PHP
创建你自己的 PHP 函数非常容易。我们从一个简单的示例开始,然后再详细阐述它的工作原理。假设你想创建一个 PHP 函数,当你在浏览器中调用它时,它会简单的写一条消息。
Its very easy to create your own PHP function. Let’s start with a simple example after which we will elaborate how it works. Suppose you want to create a PHP function which will simply write a simple message on your browser when you will call it.
Example
在这个示例中,我们创建了一个名为 writeMessage() 的函数,然后调用它来打印一条简单消息 −
In this example, we create a function called writeMessage() and then call it to print a simple message −
<?php
/* Defining a PHP Function */
function writeMessage() {
echo "You are really a nice person, Have a nice time!";
}
/* Calling a PHP Function */
writeMessage();
?>
它将生成以下 output −
It will produce the following output −
You are really a nice person, Have a nice time!
Creating a Function in PHP
现在,让我们详细了解这个过程。第一步是编写一个函数,然后你可以根据需要多次调用它。要创建一个新 function ,请使用 function 关键字,后跟你想使用的函数名称。在名称前面放上一个圆括号,它可能包含也可能不包含参数。在其后面放上一个用大括号分隔的语句块。此函数块中包含每次调用此函数时要执行的语句。
Now let’s understand the process in detail. The first step is to write a function and then you can call it as many times as required. To create a new function, use the function keyword, followed by the name of the function you may want to use. In front of the name, put a parenthesis, which may or may not contain arguments. It is followed by a block of statements delimited by curly brackets. This function block contains the statements to be executed every time the function is called.
定义函数的一般 syntax 如下:
The general syntax of defining a function is as follows −
function foo($arg_1, $arg_2, $arg_n) {
statements;
return $retval;
}
如果这个函数打算将某些结果返回给调用环境,则应在函数块中的最后一个语句中设置 return 语句。不必有 return 语句,因为即使没有它,程序流也会返回给调用者,尽管不会携带任何值。
If the function is intended to return some result back to the calling environment, there should be a return statement as the last statement in the function block. It is not mandatory to have a return statement, as even without it, the program flow goes back to the caller, albeit without carrying any value with it.
任何有效的 PHP 代码都可能显示在某个函数内部,甚至可以是其他函数和类定义。函数的名称必须遵循与用于形成变量名称相同的规则。它应以字母或下划线开头,后面可以跟任意数量的字母、数字或下划线。
Any valid PHP code may appear inside a function, even other functions and class definitions. Name of the function must follow the same rules as used to form the name of a variable. It should start with a letter or underscore, followed by any number of letters, numbers, or underscores.
下面是 PHP 中的一个简单函数。每当被调用时,它就会显示消息“你好,世界”。
Here is a simple function in PHP. Whenever called, it is expected to display the message "Hello World".
function sayhello() {
echo "Hello World";
}
Calling a Function in PHP
一旦定义了一个函数,就可以在 PHP 代码的任何地方多次调用它。请注意,函数不会自动调用。要调用某个函数,请在语句中使用它的名称;函数名称后面跟着一个分号。
Once a function is defined, it can be called any number of times, from anywhere in the PHP code. Note that a function will not be called automatically. To call the function, use its name in a statement; the name of the function followed by a semicolon.
<?php
# define a function
function sayhello(){
echo "Hello World";
}
# calling the function
sayhello();
?>
它将生成以下 output −
It will produce the following output −
Hello World
假设上述脚本 "hello.php" 出现在 PHP 服务器的文档根目录中,打开浏览器并输入 URL,如下 http://localhost/hello.php 。你应该在浏览器窗口中看到消息“你好,世界”。
Assuming that the above script "hello.php" is present in the document root folder of the PHP server, open the browser and enter the URL as http://localhost/hello.php. You should see the "Hello World" message in the browser window.
在此示例中,函数会被定义,而没有任何参数或返回值。在随后的章节中,我们将了解如何定义和传递参数,以及如何让一个函数返回一些值。还将详细解释 PHP 函数的一些高级特性,例如递归函数、按值或引用调用函数等。
In this example, the function is defined without any arguments or any return value. In the subsequent chapters, we shall learn about how to define and pass arguments, and how to make a function return some value. Also, some advanced features of PHP functions such as recursive functions, calling a function by value vs by reference, etc. will also be explained in detail.