Home  >  Article  >  Backend Development  >  What is the usage of function keyword in php

What is the usage of function keyword in php

青灯夜游
青灯夜游Original
2022-02-10 17:37:555794browse

function is a keyword in php, used for users to declare custom functions. The syntax is "function function name ([parameter 1, parameter 2, ..., parameter n]) {function body; [ return return value;]}".

What is the usage of function keyword in php

The operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer

PHP functions can be divided into two types, namely PHP's predefined functions and user-defined functions. Users can directly use predefined functions in their own programs or PHP files. PHP provides a large number of feature-rich predefined functions for PHP developers to use, which greatly improves development efficiency. Custom functions are functional modules used by developers to solve specific needs.

And function is the keyword used to declare custom functions in PHP.

To declare a custom function in PHP, you can use the following syntax format:

function 函数名 ([参数1, 参数2, ..., 参数n]){
    函数体;
    [return 返回值;]
}

The syntax format of the function is as follows:

  • The first line of each function is the function header, which consists of three parts: the keyword function declaring the function, the function name, and the parameter list. Each part completes a specific function;

  • Each custom function must be declared using the function keyword;

  • The function name can represent the entire function, and the function can be named any name, as long as the naming rules for variable names are followed. Can. Each function has a unique name, but it should be noted that function overloading cannot be used in PHP, so functions with the same name cannot be defined, including the same name as the system function;

  • When declaring a function, the parentheses "()" after the function name are also required. The parentheses contain a set of acceptable parameter lists. The parameters are the declared variables, and then the variables can be passed to when calling the function. function. The parameter list can be empty or have one or more parameters. Use commas to separate multiple parameters;

  • You need to use a space between the keyword "function" and the function name. Separated, and there is no need to use spaces to separate the function name and the parentheses wrapping the parameter list. Of course, adding spaces will not cause an error;

  • The function body is located after the function header and needs to be Use curly brackets "{}" to wrap it. All the work of a function is done in the function body. After the function is called, the first statement in the function body is first executed, and execution ends after the return statement or the outermost brace "}", and returns to the place where the function was called. Any valid PHP code can be used in the function body, and even the definition of other functions or classes can be declared in the function body;

  • Use the keyword return to return a Value or expression, when the program executes to the return statement, the expression will be calculated, and then return to the place where the function was called to continue execution.

Because the parameter list and return value are not required when defining a function, but other parts are required, there are usually the following ways to declare a function.

1) There is no parameter list when declaring a function:

function 函数名(){
    函数体;
    return 返回值;
}

2) There is no return value when declaring a function:

function 函数名(参数1, 参数2, ..., 参数n){
    函数体;
}

3) There is no need when declaring a function Parameter list and return value:

function 函数名(){
    函数体;
}

Call of function

Whether it is a custom function or a system function, if the function is not called, it will not be executed. Just call the function using its name and parameter list wherever you need to use it.

After the function is called, it starts executing the code in the function body. After the execution is completed, it returns to the calling position and continues downward execution. Therefore, the function name can summarize the following three functions when the function is called.

  • You can call the function through the function name and let the code of the function body run. The function body will be executed several times after calling it several times;

  • If the function has a parameter list, you can also pass the corresponding value to the parameter in parentheses after the function name, and use the parameters in the function body to change the execution behavior of the internal code of the function;

  • If the function has a return value, when the function is executed, the value after return will be returned to the location where the function was called, so that the function name can be used as the value returned by the function.

Tip: As long as the declared function is visible in the script, it can be called anywhere in the script through the function name. In PHP, it can be called after the function is declared. You can also call a function before its declaration, or you can call a function within a function.

[Example] When we explain the for loop, the program that prints the multiplication table is encapsulated into a function. The code is as follows:

<?php
    function table(){       //定义函数
        for ($i = 1; $i <= 9; $i++) {
            for ($j = 1; $j <= $i; $j++) {
                echo $j.&#39; * &#39;.$i.&#39; = &#39;.$i*$j.&#39;  &#39;;
            }
            echo &#39;<br>&#39;;
        }
    }
    table();//调用函数
?>

The running results are as follows:

1 * 1 = 1 
1 * 2 = 2  2 * 2 = 4 
1 * 3 = 3  2 * 3 = 6  3 * 3 = 9 
1 * 4 = 4  2 * 4 = 8  3 * 4 = 12  4 * 4 = 16 
1 * 5 = 5  2 * 5 = 10  3 * 5 = 15  4 * 5 = 20  5 * 5 = 25 
1 * 6 = 6  2 * 6 = 12  3 * 6 = 18  4 * 6 = 24  5 * 6 = 30  6 * 6 = 36 
1 * 7 = 7  2 * 7 = 14  3 * 7 = 21  4 * 7 = 28  5 * 7 = 35  6 * 7 = 42  7 * 7 = 49 
1 * 8 = 8  2 * 8 = 16  3 * 8 = 24  4 * 8 = 32  5 * 8 = 40  6 * 8 = 48  7 * 8 = 56  8 * 8 = 64 
1 * 9 = 9  2 * 9 = 18  3 * 9 = 27  4 * 9 = 36  5 * 9 = 45  6 * 9 = 54  7 * 9 = 63  8 * 9 = 72  9 * 9 = 81

[Example] Let's define a function to implement simple addition operation. The code is as follows:

<?php
    function add($num1,$num2){
        $a = $num1 + $num2;
        return $a;
    }
    $sum = add(11,5);
    echo &#39;$sum = &#39;.$sum.&#39;<br>&#39;;
    echo &#39;6 + 33 =&#39;.add(6,33).&#39;<br>&#39;;
    echo &#39;42 + 21 =&#39;.add(42,21).&#39;<br>&#39;;
    echo &#39;167 + 153 =&#39;.add(167,153);
?>

The running results are as follows:

$sum = 16
6 + 33 =39
42 + 21 =63
167 + 153 =320

Recommended learning: " PHP video tutorial

The above is the detailed content of What is the usage of function keyword in php. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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