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;]}".
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.' * '.$i.' = '.$i*$j.' '; } echo '<br>'; } } 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 '$sum = '.$sum.'<br>'; echo '6 + 33 ='.add(6,33).'<br>'; echo '42 + 21 ='.add(42,21).'<br>'; echo '167 + 153 ='.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!

The article compares ACID and BASE database models, detailing their characteristics and appropriate use cases. ACID prioritizes data integrity and consistency, suitable for financial and e-commerce applications, while BASE focuses on availability and

The article discusses securing PHP file uploads to prevent vulnerabilities like code injection. It focuses on file type validation, secure storage, and error handling to enhance application security.

Article discusses best practices for PHP input validation to enhance security, focusing on techniques like using built-in functions, whitelist approach, and server-side validation.

The article discusses strategies for implementing API rate limiting in PHP, including algorithms like Token Bucket and Leaky Bucket, and using libraries like symfony/rate-limiter. It also covers monitoring, dynamically adjusting rate limits, and hand

The article discusses the benefits of using password_hash and password_verify in PHP for securing passwords. The main argument is that these functions enhance password protection through automatic salt generation, strong hashing algorithms, and secur

The article discusses OWASP Top 10 vulnerabilities in PHP and mitigation strategies. Key issues include injection, broken authentication, and XSS, with recommended tools for monitoring and securing PHP applications.

The article discusses strategies to prevent XSS attacks in PHP, focusing on input sanitization, output encoding, and using security-enhancing libraries and frameworks.

The article discusses the use of interfaces and abstract classes in PHP, focusing on when to use each. Interfaces define a contract without implementation, suitable for unrelated classes and multiple inheritance. Abstract classes provide common funct


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

Zend Studio 13.0.1
Powerful PHP integrated development environment

SublimeText3 Mac version
God-level code editing software (SublimeText3)

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool