PHP simple learning function

Custom function

The format of the custom function:
function function name { code block; }


The execution of the function can be directly put into the function name
. The above example
Insert picture description here
Moreover, once defined, the full text uses
such as

formal parameters and actual parameters.
This example is a bit sloppy, but I believe you must Can understand

Insert picture description here
Of course, parameters can also be variables

The return value
return returns the value
to the caller as shown in the
figure below. After the superd1ng(2,3) function is called, the function superd1ng returns the value of 2+3 to superd1ng.
If you don’t write it, it returns null.

Insert picture description hereThe second role of return is to end the operation of the function.
Insert picture description here
As shown in the figure above, the output $a in the function has not been executed.

variable

Local variables
are only executed inside the function, and they are wrong after being executed outside the function
. $a is empty as shown in the figure below
Insert picture description here

Global variables, as the
name suggests, are globally effective, but are useless inside the function. If you need it in the function, you can use the global function
define() a constant, which can be used in the function.
Insert picture description here
The static variable
static is
only defined in the function, but not stored in the function, but it will not be cleared immediately after the function is called, and the line of code that defines the initial value is only run once, and the function will not be executed again if the function is called again. Code again.

Insert picture description here
As shown in the figure, when the function is called for the second time, the value of $a still exists and is the value of the result of the last call

Passing of parameters

1. Passing parameters by value (default)
The operation of formal parameters will not affect the actual parameters.

Passing parameters by reference is
just the opposite of the above figure. The operation of formal parameters will affect the actual parameters. You
only need to specify the parameters when defining the function. Add an ampersand before it. The default parameters of the function are
as follows
Insert picture description here
. Assign a value to $a in the defined function. When calling, if no value is passed in, the value assigned during definition will be calculated. If a value is input, the input value will be calculated. And follow a rule, assign values ​​from right to left because when calling a function assignment, you can only assign values ​​from left to right. 4. Variable length parameter sequence

Insert picture description here
Insert picture description here

Insert picture description here


Insert picture description here

It can return the input, the input value, but the data of the array type
func_get_args() type.
As shown in the figure below, return the input value
Insert picture description here
func_get_arg() type.
I don’t talk nonsense, directly above the picture, easy to understand
Insert picture description here
Insert picture description here
Insert picture description here
Insert picture description here
3. func_num_args() type
output, input The number of values, as shown in the figure.
Insert picture description here

Variable function (variable function)

Assign the function to the variable.
As shown in the figure below,
just enclose the function name in single quotation marks to assign it to the variable.
Insert picture description here

recursive function

Inside the function, call your own function yourself. Similar to the C language
Insert picture description here
, the above figure is the principle as follows:
first output 2, and then judge whether 2 is greater than 0, and then call the function text here, and then judge with 0, and then call the function text, at this time the call value is 0, not greater than 0, output the content after the else, and then return the value. (It's really a bit convoluted here, I don't understand why the return value is required to ask the big guys to clarify)

Internal function

PHP provides many built-in or ready-made structures that we can directly use in actual development.
On-demand understanding

Guess you like

Origin blog.csdn.net/qq_51954912/article/details/114106561