In the languages I know, the function command is used to define functions. next we will introduce the usage of functions in php. User-defined functions are also called Udfs. they are not provided by PHP and are created by programmers.
In the languages I know, the function command is used to define functions. next we will introduce the usage of functions in php.
User-defined functions are also called Udfs. they are not provided by PHP and are created by programmers. Because you have created such functions, you can fully control these functions. Therefore, a function can be run in the desired way.
1. declare a function
In PHP, the methods for defining functions are almost the same as those in other programming languages. The syntax structure of the PHP declarative function is as follows:
The instance code is as follows:
- Function function_name ($ argument1, $ argument2, $ argument3 ,...... $ Argumentn)
- {
- // Function code
- Return value;
- }
In the above syntax structure, the meaning of the keyword is as follows.
(1) function: keywords used to declare user-defined functions.
(2) function_name: name of the function to be created. This name will be used in future calls. The function name must be unique because PHP does not support overloading. When naming a function, you must follow the variable name.
The same principle. However, the function name cannot start with $, and the variable can.
(3) argument: the value to be passed to the function. A function can have multiple parameters with commas. However, parameter items are optional. you can call a function without passing any parameters.
(4) code: A piece of code executed when the function is called. If there are two or more statements, the code must be enclosed in braces. However, if there is only one code, braces are not required.
(5) Return: Return the value required by the called code. All types can be returned, including lists and objects. This causes the function to immediately end its operation and pass control back to the called row.
2. no parameter functions
Code func_1. Php
The instance code is as follows:
-
- Require 'a. Php ';
- Echo "I won't be executed! ";
- ?>
3. parameter functions
PHP supports passing parameters by value (default) through reference and default parameter values. The variable length parameter list is only supported in PHP4 and later versions.
(1) value transfer parameters. Passing parameters by value is the default transfer method of PHP. To use this method, you must pass a value (parameter) when calling the main program ).
Code sum. Php
The instance code is as follows:
-
- Function sum ($ a, $ B)
- {
- Echo $ a + $ B;
- }
- Sum (); // start to call this function
- ?>
(2) reference parameters. When passing by value, only a copy of the parameter is passed to the called function. However, any modification to these values within the called function does not affect the call of the function.
The original value in. The reference transfer is actually the address transfer, and the address of a variable is passed as a parameter.
Code valuechange. Php
The instance code is as follows:
-
- $ MyNum = 100;
- Function Valuechange ($ number)
- {
- $ Number = $ number + 1;
- Echo $ number. "
";
- }
- Valuechange ($ myNum );
- Echo $ myNum;
- ?>
(3) default value parameter. When using the default parameter value transfer method, the function must have a parameter when calling. If no value is used, the default value is passed to the function parameter. The default value must be a constant expression, not a variable,
Class member or function call.
Tip: When using default parameters, any default parameters must be placed on the right of non-default parameters; otherwise, the function may not work as expected.
Code func_default. Php
The instance code is as follows:
-
- $ MyNum = 100;
- Function Valuechange ($ number)
- {
- $ Number = $ number + 1;
- Echo $ number. "
";
- }
- Valuechange ($ myNum );
- Echo $ myNum;
- ?>