This article describes the functions of PHP.
How to learn it? Can be considered from the following aspects
How is the function defined? Is it case-sensitive? How are the parameters of a function defined? Does the function support overloading? How the return value of the function is defined. Does the function have variable functions? If the above problem is clear, believe the function you also mastered. Let's look at it.
How is the function defined? Is it case-sensitive? First, the function is not sensitive to case. However, it is recommended that you use the same as when declaring a function.
How is the function defined? The syntax can be:
function func ($arg _1, $arg _2,, $arg _n)
{
echo "Example function.\n";
return $retval;
}
?>
In fact, it is similar to other languages. However, there is no need to explicitly describe the return type in the function declaration. Same as JavaScript.
So is it the same as C, where the function is defined and then used? The problem is very good. This is true in PHP3, but there is no limit to the later version.
Since PHP exists function or conditional function of functions, so these 2 cases need to be defined before use, if you do not define the function you use, the system will be problematic. The functions in the function are somewhat similar to Python.
Examples of conditional functions can be:
1 2 $isRequired = true;
3 if ($isRequired)
4 {
5 function func ($op 1, $op 2)
6 {
7 return $op 1 + $op 2;
8}
9}
if ($isRequired)
echo "func (1,3) =". Func (1, 3);
12
function HelloWorld ()
14 {
return "Hello,world";
16}
Echo '
Call function HelloWorld (): '. HelloWorld ();
?>
The output is:
Func (1, 3) = 4
Call function HelloWorld (): Hello, World
The functions in the function can be:
1 2 function func ()
3 {
4 function Subfunc ()
5 {
6 echo "I don t exist until Func () is called.\n";
7 echo "I have alrady made";
8}
9}
10
/* We can ' t call Subfunc () yet
Since it doesn ' t exist. */
13
Func ();
15
/* Now we can call Subfunc (),
+ func () ' s processesing has
Made it accessable. */
19
Subfunc ();
21st
?>
The output is:
I don ' t exist until Func () is called. I have alrady made
2. How are the parameters of the function defined?
The parameter list is separated by commas, just like the function parameters that are normally used. So is the argument passed by value or by reference? The answer is value passing. How do I pass it by reference? In fact, as in C + +, use the & symbol before the parameter.
So how do you set the default parameter values? This is the same as C + +, which is written directly in the parameter list. For example:
function Makecomputerbrand ($brand = "IBM")
{
Return "Making". $brand. "Computer now.
" ;
}
Echo Makecomputerbrand ();
Echo Makecomputerbrand ("DELL");
Echo Makecomputerbrand ("HP");
Echo Makecomputerbrand ("Lenevo");
?>
The result of the output is:
Making IBM Computer Now.
Making DELL Computer Now.
Making HP Computer Now.
Making Lenevo Computer Now.
3. Does the function support overloading?
Not supported.
4. How the function's return value is defined.
If you return a value individually or do not return a value, as in normal language, return is possible. However, if more than one value is returned, one method is to return an array. For example:
function Small_numbers ()
{
Return Array (0, 1, 2);
}
List ($zero, $one, $two) = Small_numbers ();
?>
5. Do functions have variable functions?
Have, as with mutable variables.