Home > Backend Development > PHP Tutorial > PHP study notes: definition and calling of functions

PHP study notes: definition and calling of functions

WBOY
Release: 2023-10-08 14:00:01
Original
1115 people have browsed it

PHP study notes: definition and calling of functions

PHP study notes: Definition and calling of functions

In PHP, a function is a block of code that can be reused. Functions can improve the readability and reusability of code and make the code more modular. This article will introduce how to define and call PHP functions, and give specific code examples.

1. Definition of function

In PHP, the keyword function is used to define a function. The general syntax of function definition is as follows:

function functionName(parameters) {
    // 函数的代码块
}
Copy after login

where functionName is the name of the function, which can be named according to actual needs. parameters are the parameters of the function, and different parameters can be passed according to actual needs.

The following is a simple example that defines a function that calculates the sum of two numbers:

function sum($num1, $num2) {
    $result = $num1 + $num2;
    return $result;
}
Copy after login

In the above example, sum is the name of the function, $num1 and $num2 are the parameters of the function. The code inside the function is the logic to calculate the sum of the two parameters. Finally, the calculated result is returned through the return statement.

2. Function call

The function call is implemented by passing the function name and parameters. The general syntax for calling a function is as follows:

functionName(arguments);
Copy after login

An example of calling the sum function defined above is as follows:

$num1 = 10;
$num2 = 20;
$total = sum($num1, $num2);
echo $total;
Copy after login

In the above example, two variables are first defined$num1 and $num2, and then calculate the sum of the two numbers through the function call sum($num1, $num2), and assign the calculated result to the variable $total. Finally, use the echo statement to output the calculation results to the page.

3. Specific code examples

The following is a more complete example, showing the definition and calling of functions:

// 定义函数:计算两个数的乘积
function multiply($num1, $num2) {
    $result = $num1 * $num2;
    return $result;
}

// 调用函数
$num1 = 5;
$num2 = 2;
$product = multiply($num1, $num2);

echo "两个数的乘积是:" . $product;
Copy after login

In the above example, first define a Function multiply is used to calculate the product of two numbers. Then, by calling the function, the product of the two numbers is calculated and the result is assigned to the variable $product. Finally, use the echo statement to output the calculated results.

Summary:

This article introduces the definition and calling of functions in PHP. A function is a block of code that can be reused, making the code more readable and reusable. Through specific code examples, readers can better understand the definition of functions and how to use them. I hope this article can be helpful to beginners learning PHP functions.

The above is the detailed content of PHP study notes: definition and calling of functions. 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