PHP function basic syntax custom function
PHP provides powerful functions, but this is far from meeting the needs. Programmers can create functions by themselves as needed. In this section, we begin to learn how to create functions.
In the actual development process, we need to have many functions that need to be used repeatedly. If we can define these functions that need to be used repeatedly, we can define them as functions (functions) as much as possible ( function). When using it, just shout its name.
Then let’s learn the grammatical provisions of custom functions. The grammatical provisions are as follows:
function 函数名([参数名1[=值1], 参数名2[=值2], 参数名n[=值n]]) { 函数中的功能体 [return 返回值] }
The following characteristics are found in the above grammatical provisions, resulting in the following unspecified expression:
1. The function starts with function
2. Function is followed by a space, and the space is followed by the function name
3. The naming rules for function names and variables are basically the same, but the difference is: function name It is not case sensitive
4. The so-called parameters are actually variables
5. The function name is followed by brackets, and the parameters are enclosed in brackets. All parameters are enclosed by [] (square brackets), which means The parameters can be filled in or not
6. If there are parameters, the parameters can be followed by an equal sign (=), and the equal sign is followed by the default value. Parameter values are also enclosed in [] (square brackets), indicating optional
7. The main function of the parameter variables after the function is to pass the variable values outside the function into the function body for use. The function body The variables and the variables outside the function are usually two different variables.
8. The specific function (function body) in the function is enclosed in curly brackets, indicating that this is the function range of a function
9. A function can have a return value or no return value. Those enclosed by [] (square brackets) represent optional fields.
10. Return is followed by a space, and a space is followed by the return value. If there is a return, the code after the return will not be executed.
11. There is no order relationship in the execution of functions. They can be called before the definition.
12. A function cannot be defined twice, that is, the function cannot be overloaded
Remember: You also need to write more code to conduct experiments!
We can prove these points bit by bit through experiments.
1. The function is like a dog. Wherever it is called, it will follow you.
php_cnDoes this function display three paragraphs: I am A dog?
2 Function namecan only be a combination of letters, numbers, and underscores, and cannot contain spaces. Numbers cannot be placed at the beginning of the variable name.
The above code will report an error. Function naming and variable naming
3. Function names and variable naming rules are the same, but the difference is: function names are not case-sensitive
'; } demo(); Demo(); DEMO(); ?>
Through the above code, you will find that three lines will be displayed: If it is a man who writes the code, we need to take more responsibility from the family
.
That is, the function name is not case-sensitive when called. A function can have its name called repeatedly and be executed repeatedly. It also reflects the characteristics of reuse.
4. If the parameters of the function body are defined and no parameters are passed, the code will report an error
Write a paragraph yourself and try it. Will it report an error?
5. If the parameters after the function have default values, the parameters do not need to be filled in, and the code will not report an error.
Is there no error reported? And it showed 10 and came out.
Note that if parameters are passed, they will be brought into the function. If the function does not pass parameters, the default value after the parameters will be used.
6. You can write multiple parameters after the function
Write the above code yourself. Have we passed in multiple parameters?
7. If there are parameters with default values and no default values after the function, the parameters without default values are usually written in the first 3rd and 3rd numbers before
From 4 experiments, we found that the default value means that this parameter does not need to be passed in. If there is no default value, the code will report an error, that is, parameters without default values must be passed. Let's take a look at the following example:
Through the above example, we found that another error was reported when executing the above code. That is to say, the above syntax will not work.
What we hope in the above syntax is that we do not pass in any values for parameter $a and parameter $b. The parameter $c must be passed in, we passed 8. But PHP's grammatical regulations do not allow us to write this. Therefore, we can achieve the same effect by changing the way of writing:
Through the above example experiment, did you find that I passed the parameter $c and the code did not report an error. The parameter $a = 20 and the parameter $b = 30 are also brought into the code $a + $b + $c.
8. There is no relationship between the variables in the function body and the variables outside the function
Through the above example, we found that there is no change point relationship between the formal parameters and the actual parameters. No matter how the formal parameters passed in the function body change, it will not affect the actual result of the actual parameter $hello.
9. If there is a return in the function body, the code after the return will not be executed.
Have you noticed? ——Only 111 is output.
10. After the function is executed, return can bring the value in the function body out of the function body
11. There is no order relationship in the execution of functions. It can be called before the definition
Through the above example, we found that the function can be called before or after the definition. That is, the function can be called anywhere.
11. Functions cannot be overloaded
It is found through the above example that an error is reported, that is, the function with the same name cannot be defined twice, otherwise an error is reported
Assignment:
1. Define a double-layered loop with alternating rows of color-changing tables
2. Require this form, the default values are 10 and 10, one of the colors is a default parameter, and one has no default parameters
3. Return the string of the table back