This article introduces the usage of PHP custom functions in detail, including 1. The function name cannot have the same name as an existing function name.
2. Function names can only contain letters, numbers and underscores.
3. The function name cannot start with a number
Custom function
7.2.1 Basic principles of function naming:
1. The function name cannot have the same name as an existing function name.
2. Function names 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 |
代码如下 |
复制代码 |
//创建函数
function funcCountArea($radius)
{
return $radius*$radius*pi();
}
//使用函数
$area = funcCountArea(20);
echo $area;
echo ' ';
$area2 = funcCountArea(30);
echo $area2;
?>
output
1256.63706144
2827.43338823
|
//Create function
代码如下 |
复制代码 |
$a = 5;
function funcChange($a)
{
$a = 2 * $a;
}
funcChange ($a);
echo $a;
?>
output
5
|
function funcCountArea($radius)
{
代码如下 |
复制代码 |
$a = 5;
function funcChange(&$a)
{
$a = 2 * $a;
}
funcChange ($a);
echo $a;
?>
output
10
|
return $radius*$radius*pi();
}
代码如下 |
复制代码 |
function funcUserInfo($username,$password)
{
$userInfo = array($username,$password);
return $userInfo;
}
$arr = funcUserInfo('anllin','123456');
print_r($arr);
?>
output
Array ( [0] => anllin [1] => 123456 )
|
//Use function
$area = funcCountArea(20);
echo $area;
echo ' ';
$area2 = funcCountArea(30);
echo $area2;
?>
output
1256.63706144
2827.43338823
7.2.3 Passing parameters by value
The code is as follows |
Copy code |
<🎜>$a = 5;<🎜>
<🎜>function funcChange($a)<🎜>
<🎜>{<🎜>
<🎜> $a = 2 * $a;<🎜>
<🎜>}<🎜>
<🎜>funcChange ($a);<🎜>
<🎜>echo $a;<🎜>
<🎜>?>
output
5
|
7.2.4 Passing parameters by reference
The code is as follows |
Copy code |
<🎜>$a = 5;<🎜>
<🎜>function funcChange(&$a)<🎜>
<🎜>{<🎜>
<🎜> $a = 2 * $a;<🎜>
<🎜>}<🎜>
<🎜>funcChange ($a);<🎜>
<🎜>echo $a;<🎜>
<🎜>?>
output
10
|
7.2.5 Function calls that return multiple values
The code is as follows |
Copy code |
<🎜>function funcUserInfo($username,$password)<🎜>
<🎜>{<🎜>
<🎜> $userInfo = array($username,$password);<🎜>
<🎜> return $userInfo;<🎜>
<🎜>}<🎜>
<🎜> <🎜>
<🎜>$arr = funcUserInfo('anllin','123456');<🎜>
<🎜> <🎜>
<🎜>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
代码如下 |
复制代码 |
function funcUserInfo($username, $password)
{
$userInfo [] = $username;
$userInfo [] = $password;
return $userInfo;
}
$arr[] = funcUserInfo ( 'Bob', '512655' );
$arr[] = funcUserInfo ( 'John', '458736' );
$arr[] = funcUserInfo ( 'Mark', '925472' );
print_r ( $arr );
?>
output
Array ( [0] => Array ( [0] => Bob [1] => 512655 ) [1] => Array ( [0] => John [1] => 458736 ) [2] => Array ( [0] => Mark [1] => 925472 ) )
|
|
Copy code
|
| function funcUserInfo($username, $password)
{
$userInfo [] = $username;
$userInfo [] = $password;
return $userInfo;
}
$arr[] = funcUserInfo ( 'Bob', '512655' );
$arr[] = funcUserInfo ( 'John', '458736' );
$arr[] = funcUserInfo ( 'Mark', '925472' );
print_r ( $arr );?>
output
Array ( [0] => Array ( [0] => Bob [1] => 512655 ) [1] => Array ( [0] => John [1] => 458736 ) [2] => Array ( [0] => Mark [1] => 925472 ) )
Note: Function calls are not case-sensitive, but variable names are case-sensitive.
http://www.bkjia.com/PHPjc/629233.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/629233.htmlTechArticleThis article introduces the usage of PHP custom functions in detail, including 1. The function name cannot be the same as the existing function name Duplicate name. 2. Function names can only contain letters, numbers and underscores. 3. The function name cannot start with a number...
|