The format of functions defined in PHP is: function function_name(parameters) {//Function body}
##Defined in PHP Function
Definition format:
<code class="php">function function_name(parameters) {
// 函数体
}</code>
Copy after login
Description:
##function_name- : Function name, consisting of letters, numbers or underscores, and cannot start with numbers.
parameters- : Optional, list of parameters received by the function. Each parameter consists of its type and name, separated by commas.
Function body- : Contains the code to be executed by the function.
Example:
<code class="php">// 无参函数
function hello() {
echo "Hello, world!";
}
// 有参函数
function sum(int $a, int $b) {
return $a + $b;
}</code>
Copy after login
Rules:
Function names must be unique.
- The parameter type and number must be consistent with the function definition.
- Code in the function body can access variables outside the function, but cannot modify them.
- A function can return a value using the
return- statement.
The above is the detailed content of How to define a function in php. For more information, please follow other related articles on the PHP Chinese website!