Home  >  Article  >  Backend Development  >  Teach you step by step how to use PHP functions (selected)

Teach you step by step how to use PHP functions (selected)

慕斯
慕斯Original
2021-06-02 18:09:532603browse

The previous article introduced you to "How to quickly learn PHP operators in one day (detailed introduction)". This article continues to introduce to you what is a function? I’ll teach you how to use PHP functions step by step (selected). If you don’t hurry up and learn, you won’t be able to learn if you’re late! ! !

Teach you step by step how to use PHP functions (selected)

#What is a function?

A function on a computer is a series of expressions. In fact, there is a program. When you input the corresponding parameters, the result will appear after passing the function. It also refers to the implementation done inside the compiler. A piece of code for a certain function allows you to use a very simple statement to implement a very complex operation;

The syntax structure of a custom function:

Basic format:

function 函数体(){
    PHP语句(函数体)
 }

1: The last name of the function is function and cannot be omitted

2: Function name

  • can be English, but cannot be Chinese

  • can be a number, but cannot start with a number

  • cannot use special characters. _Except

  • Function names are not case-sensitive

  • Function declaration must be meaningful

  • No Can have the same name as an existing function ()

##3: Features of the function

  • After the function is defined, no The call is not executed;

  • The function calls are in no particular order because the PHP parser has pre-loaded the content of the function before executing the code;

Specifically, we take the code as an example:

<?php
    function demo(){
        echo &#39;我是函数体&#39;;
    }
    demo();
?>

The execution results are as follows:

Teach you step by step how to use PHP functions (selected)

## Regarding other forms of functions, they are listed as follows:

1. Pass parameters by value

(By default, we use this method when passing parameters)The operation of formal parameters will not affect to the value of the actual parameter.

2. Pass parameters by reference

Pass parameters by reference, and the operation of the formal parameters will affect the actual parameters

3. Default parameters of functions

You can set default values ​​for formal parameters by directly assigning values ​​(assign values ​​from right to left, the right one must be present before you can assign a value to the left one)

4. Variable length parameter list

Provided by Php, you can use it directly

Func_get_args();//Similar to an array

Func_get_arg();//When calling this function, you can pass in a value to indicate which actual parameter is obtained (starting from 0)

Func_num_args();//Return the custom function parameters passed in The number

These three functions can be used inside our custom functions and can return us some information about the parameters

5. Variable functions (variable functions)

Assign functions directly to variables

Variable functions cannot be used in languages ​​such as echo, print, unset(), isset(), empty(), include, require and similar languages structure. Your own wrapper function is required to use these structures as variadic functions.

6. Recursive function

That is, calling your own function inside the function

1) The function will be in the memory during the process of being called and executed. The space allocated inside is used to store temporary data, so there is no connection between functions by default during execution (except for static variables, passed by reference, and global variables). The variables inside are local variables by default and have no influence on each other

2) Recursive functions need to have conditions within the function to end the function in a timely manner

Recommended learning:

php video tutorial

The above is the detailed content of Teach you step by step how to use PHP functions (selected). 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