PHP function learning route from entry to advanced

WBOY
Release: 2023-06-15 22:34:01
Original
1190 people have browsed it

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.

  1. Basic knowledge of functions

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;
}
Copy after login

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 definition: Use the 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.
  • Function call: When calling a function, pass parameters to the function. Functions can use these parameters to perform some operations and give results.
  • Function return: The function uses the 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.
  1. Common types of functions

In PHP, there are many types of functions available. Here are some commonly used types:

  • System functions: PHP has some built-in functions that can be called directly. For example, the echo() function outputs text to the page, and the date() function gets the current date and time.
  • Custom functions: You can create your own functions to perform custom operations. For example, the add() function above is a custom function.
  • Anonymous functions: Also known as closures, you can dynamically create a function and assign it to a variable. This function has no name and can be called using variable names. For example, $add = function($a, $b) { return $a $b; }, which can then be called using $add(1, 2).
  • Variable functions: Also known as variable functions, they are functions that are called by string or variable values. For example, $func_name = 'add'; $func_name(1,2), this will call the add() function and pass the parameters.
  1. Parameter passing of function

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
Copy after login

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
Copy after login

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.

  1. Variable scope of functions

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
Copy after login

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
Copy after login

In this example, we use the global key Mark $x as a global variable and use it within function test().

  1. Advanced function concepts

There are also some advanced function concepts that can expand the usage of PHP functions:

  • Anonymous functions (already mentioned above to)
  • Closure: A closure is a self-contained function that can access variables within its enclosing scope. This allows us to create real functional modules. For example:
function add($x) {
    return function($y) use ($x) {
        return $x + $y;
    };
}

$add5 = add(5);
echo $add5(3); // 输出 8
echo $add5(4); // 输出 9
Copy after login

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);
Copy after login

在这个例子中,我们使用了一个回调函数 $print,它接收一个参数并将其输出到屏幕上。然后我们使用 process() 函数,将 $print 函数作为参数传递,并将 $data 数组作为参数。在 process() 函数内部,我们使用 foreach() 循环遍历 $data 数组,并将每个元素传递给 $func 函数,这里就是 $print 函数。

  • 可变参数:可变参数,在PHP中称为“不定数量的参数”,是指一个函数可以接受任意数量的参数。使用可变参数时,可以将一个函数设计为可接受任意数量的参数,而无需知道有多少个参数或它们的名称。
function add(...$nums) {
    return array_sum($nums);
}

echo add(1, 2, 3, 4, 5, 6); // 输出 21
Copy after login

在这个例子中,我们定义一个 add() 函数,使用三个点号(...)作为前缀来指示它是一个可变参数函数。然后我们使用 array_sum() 函数对所有传递给函数的值进行求和。

  1. 总结

本文介绍了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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template