Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use the function keyword in php

2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

Today, the editor will share with you the relevant knowledge points about how to use the function keyword in php. The content is detailed and the logic is clear. I believe most people still know too much about this knowledge, so share this article for your reference. I hope you can get something after reading this article. Let's take a look.

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

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

The functions of PHP can be divided into two types, namely, PHP predefined functions and user-defined functions. Users can use predefined functions directly in their own programs or PHP files. PHP provides a large number of rich predefined functions for PHP developers to use, which greatly improves the development efficiency. The custom function is a functional module that developers specifically use to solve specific requirements.

Function is the keyword used to declare custom functions in php.

Declaring a custom function in PHP can use the following syntax format:

Function function name ([parameter 1, parameter 2,..., parameter n]) {function body; [return return value;]}

The syntax format of the function is described as follows:

The first line of each function is the function header, which consists of three parts: the keyword function, the function name, and the argument list, each of which performs a specific function.

Each custom function must be declared using the function keyword

The function name can represent the entire function, and you can name the function any name, as long as you follow the naming convention of the variable name. Each function has a unique name, but it should be noted that function overloading cannot be used in PHP, so functions with duplicate names cannot be defined, including functions that cannot have the same name as system functions

The parenthesis "()" after the function name is also necessary when declaring the function, which contains a list of acceptable parameters, which are the declared variables, and then can be passed to the function when the function is called. The parameter list can be empty or have one or more parameters separated by commas

The keyword "function" and the function name need to be separated by a space, while the function name and the parentheses of the package parameter list do not need to be separated by a space. Of course, adding a space will not make an error.

The function body is located after the function header and needs to be wrapped in curly braces "{}". All the work used by the function is done in the function body. After the function is called, it first executes the first statement in the function body, ends with the return statement or the outermost curly braces "}", and returns to the place where the function was called. Any valid PHP code can be used in the function body, and even other function or class definitions can be declared in the function body

Using the keyword return, you can return a value or expression from a function. When the program executes to the return statement, the expression is evaluated and then returned to the place where the function is called to continue execution.

Because parameter lists and return values are not required when defining a function, while other parts are necessary, there are usually several ways to declare a function.

1) you can declare a function without a parameter list:

Function function name () {function body; return return value;}

2) when declaring a function, there can be no return value:

Function function name (parameter 1, parameter 2,..., parameter n) {function body;}

3) you can declare a function without a parameter list and return values:

Function function name () {function body;}

Function call

Whether it is a custom function or a system function, if the function is not called, it will not be executed. As long as you need to use the function, use the function name and argument list to make the call.

After the function is called, it starts to execute the code in the function body, and after execution, it returns to the location of the call and continues to execute downward. So the function name can summarize the following three functions when the function is called.

The function can be called through the function name, and the code of the function body can be run, and the function body will be executed several times after calling it several times.

If the function has a parameter list, you can also change the execution behavior of the internal code of the function by passing the corresponding value to the parameter in parentheses after the function name, and using the parameter in the function body

If the function has a return value, the value after return is returned to the calling function when the function finishes execution, 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, it can be called after or before the function declaration in PHP, and the function can also be called in the function.

[example] package the program that prints the ninety-nine multiplication table when we explain the for loop into a function, the code is as follows:

The running results are as follows:

1 * 1 = 11 * 2 = 22 * 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 a simple addition operation. The code is as follows:

The running results are as follows:

Sum = 16633 = 3942 + 21 = 63167 + 15320 is all the content of this article entitled "how to use the function keyword in php". Thank you for reading! I believe you will gain a lot after reading this article. The editor will update different knowledge for you every day. If you want to learn more knowledge, please pay attention to the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report