Home > Article > Backend Development > PHP function learning route from entry to advanced
PHP (Hypertext Preprocessor) is a server-side scripting language widely used in web development. It creates dynamic pages, websites, and applications, supports a variety of databases, and is easy to learn and use. Among them, function is an important concept in PHP language. This article will introduce the learning route of PHP functions from basic entry to advanced.
A function is a piece of encapsulated code that accepts some parameters, performs some operations, and returns a result. For example:
function add($a, $b) { return $a + $b; }
This function accepts two parameters and returns their sum. The above function can be called repeatedly in other places, thereby improving the reusability and maintainability of the code. Here are some basic knowledge of functions:
function
keyword to define a function. The function name should be a unique identifier, conforming to the rules for variable names. Function parameters can also be defined after the function name, with multiple parameters separated by commas. return
statement to return a result. If the return
statement does not provide a value, null
is returned. Each function can return at most one value. In PHP, there are many types of functions available. Here are some commonly used types:
echo()
function outputs text to the page, and the date()
function gets the current date and time. add()
function above is a custom function. $add = function($a, $b) { return $a $b; }
, which can then be called using $add(1, 2)
. $func_name = 'add'; $func_name(1,2)
, this will call the add()
function and pass the parameters. Parameters of function can be passed by value or by reference. When passed by value, the function uses only a copy of the argument and the original value is not modified. When passed by reference, the function takes the location of the actual arguments and makes changes to them.
Example:
function swap($a, $b) { $tmp = $a; $a = $b; $b = $tmp; } $x = 3; $y = 5; swap($x, $y); echo $x; // 输出 3 echo $y; // 输出 5
In this example, the function swap()
receives two parameters $a
and $b
, But inside the function they just use their copies. So the values of $x
and $y
are not exchanged.
If you want to exchange values, you can use passing by reference:
function swap(&$a, &$b) { $tmp = $a; $a = $b; $b = $tmp; } $x = 3; $y = 5; swap($x, $y); echo $x; // 输出 5 echo $y; // 输出 3
In this example, we have used &
symbols in the parameters, which means that the parameters are by reference transfer. Now the values of $x
and $y
have been swapped.
The variable scope in PHP includes global variables and local variables. Global variables can be used throughout the script, local variables can only be used inside functions.
Example:
$x = 10; function test() { $y = 5; echo $x; // 会抛出一个 Notice 错误 echo $y; // 输出 5 } test(); echo $x; // 输出 10
In this example, we define a local variable $y
inside the function test()
and try to The global variable $x
is used in the rest of the code. But $x
is not defined in test()
, so a Notice error will be thrown.
If you want to use global variables in a function, you can use the global
keyword:
$x = 10; function test() { global $x; $y = 5; echo $x; // 输出 10 echo $y; // 输出 5 } test(); echo $x; // 输出 10
In this example, we use the global
key Mark $x
as a global variable and use it within function test()
.
There are also some advanced function concepts that can expand the usage of PHP functions:
function add($x) { return function($y) use ($x) { return $x + $y; }; } $add5 = add(5); echo $add5(3); // 输出 8 echo $add5(4); // 输出 9
In this example, we create a closure using the add()
function, which only returns $x
and $ The sum of y
. We then use $add5 = add(5)
to create a new function and assign it to the $add5
variable. Now, $add5()
is a closure. We call it twice passing the parameters 3
and 4
respectively, and output the results after each call.
function process($data, $func) { foreach($data as $item) { $func($item); } } $print = function($x) { echo $x . "<br>"; }; $data = [1,2,3,4,5]; process($data, $print);
在这个例子中,我们使用了一个回调函数 $print
,它接收一个参数并将其输出到屏幕上。然后我们使用 process()
函数,将 $print
函数作为参数传递,并将 $data
数组作为参数。在 process()
函数内部,我们使用 foreach()
循环遍历 $data
数组,并将每个元素传递给 $func
函数,这里就是 $print
函数。
function add(...$nums) { return array_sum($nums); } echo add(1, 2, 3, 4, 5, 6); // 输出 21
在这个例子中,我们定义一个 add()
函数,使用三个点号(...
)作为前缀来指示它是一个可变参数函数。然后我们使用 array_sum()
函数对所有传递给函数的值进行求和。
本文介绍了PHP函数基础知识、常用类型、参数传递、变量作用域和高级函数概念。如果您是初学者,请从基础入门开始,逐步深入研究和实践PHP函数的使用。如果您已经有经验,请继续学习高级概念,以提高您的编程技能。无论您是什么级别的PHP程序员,掌握PHP函数是非常重要的。它是编写高效、可维护和可读的代码的关键,也是打造优秀Web应用程序的必备技能。
The above is the detailed content of PHP function learning route from entry to advanced. For more information, please follow other related articles on the PHP Chinese website!