Original: PHP 11: Functions
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:
<?Php
functionfunc ($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 <?PHP
2 $isRequired=true;
3 if($isRequired)
4 {
5 functionfunc ($op 1,$op 2)
6 {
7 return $op 1+$op 2;
8 }
9 }
Ten if($isRequired)
One Echo "func (1,3) =".func (1,3);
A
- functionHelloWorld ()
- {
the return "Hello,world";
- }
- Echo '<br>call function HelloWorld ():'.HelloWorld ();
-?>The output is:
func (1,3)=4
PagerfunctionHelloWorld ():Hello, WorldThe functions in the function can be:
1<?Php
2functionfunc ()
3{
4 functionSubfunc ()
5 {
6 Echo "I don ' t exist until Func () is called.\n";
7 Echo "I have alrady made";
8 }
9}
Ten
One/*We can ' t call Subfunc () yet
Asince it doesn ' t exist.*/
-
-func ();
the
-/*Now we can call Subfunc (),
-func () ' s processesing has
-made it accessable.*/
+
-Subfunc ();
+
A?> 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:
<?PHP
functionMakecomputerbrand ($brand = "IBM")
{
return "Making".$brand."Computer now.<br>";
}
EchoMakecomputerbrand ();
EchoMakecomputerbrand ("DELL");
EchoMakecomputerbrand ("HP");
EchoMakecomputerbrand ("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 return value of a function 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:
<?PHP
functionSmall_numbers ()
{
return Array (0, 1, 2);
}
List ($zero, $one, $two) =small_numbers ();
?>
5. Do functions have variable functions?
Have, as with mutable variables.
PHP 11: Functions