Introduction to the usage of php custom functions_PHP tutorial

WBOY
Release: 2016-07-13 17:13:09
Original
805 people have browsed it

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)

function funcUserInfo($username, $password)
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

{

$userInfo [] = $username;

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...

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template