This article describes the usage of php custom functions in detail, including 1. The function name cannot be the same as the existing function name. 2. The function name can only contain letters, numbers, and underscores. 3. The function name cannot start with a number.
Custom Functions
7.2.1 basic function naming principles:
1. The function name cannot be the same as the existing function name.
2. The function name can only contain letters, numbers, and underscores.
3. The function name cannot start with a number.
7.2.2 basic usage: declare with function
| The Code is as follows: |
Copy code |
|
<? Php
// Create a function
Function funcCountArea ($ radius)
{
Return $ radius * pi ();
}
// Use the Function
$ Area = funcCountArea (20 );
Echo $ area;
Echo '<br/> ';
$ Area2 = funcCountArea (30 );
Echo $ area2;
?>
Output
1256.63706144
2827.43338823
|
7.2.3 passing parameters by value
| The Code is as follows: |
Copy code |
|
<? Php
$ A = 5;
Function funcChange ($)
{
$ A = 2 * $;
}
FuncChange ($ );
Echo $;
?>
Output
5
|
7.2.4 passing parameters by reference
| The Code is as follows: |
Copy code |
|
<? Php
$ A = 5;
Function funcChange (& $)
{
$ A = 2 * $;
}
FuncChange ($ );
Echo $;
?>
Output
10
|
7.2.5 function call that returns multiple values
| The Code is as follows: |
Copy code |
|
<? Php
Function funcUserInfo ($ username, $ password)
{
$ UserInfo = array ($ username, $ password );
Return $ userInfo;
}
$ Arr = funcUserInfo ('anllin', '20140901 ');
Print_r ($ arr );
?>
Output
Array ([0] => anllin [1] => 123456) |
7.2.6 another function call that returns multiple values (practical: recommended)
| The Code is as follows: |
Copy code |
|
<? Php
Function funcUserInfo ($ username, $ password)
{
$ UserInfo [] = $ username;
$ UserInfo [] = $ password;
Return $ userInfo;
}
$ Arr [] = funcUserInfo ('bob', '123 ');
$ Arr [] = funcUserInfo ('john', '123 ');
$ Arr [] = funcUserInfo ('mark', '123 ');
Print_r ($ arr );
?>
Output
Array ([0] => Array ([0] => Bob [1] => 512655) [1] = & gt; Array ([0] = & gt; John [1] = & gt; 458736) [2] => Array ([0] => Mark [1] => 925472 ))
|
Note: function calls are case-insensitive, but variable names are case-sensitive.