Heim >Backend-Entwicklung >PHP-Tutorial >PHP 11:函数
本文章介绍PHP的函数。
如何学习呢?可以从以下几个方面考虑
函数是如何定义的?区分大小写吗? 函数的参数是如何定义的? 函数是否支持重载? 函数的返回值是如何定义的。 函数有变量函数吗? 如果把上面的问题搞清楚了,相信函数你也就掌握了。还是一个个看吧。
函数是如何定义的?区分大小写吗? 首先函数对大小写不敏感。但是还是建议你采用和函数声明时的一样。
函数是如何定义的呢?语法可以为:
php
function func( $arg_1 , $arg_2 , , $arg_n )
{
echo " Example function.\n " ;
return $retval ;
}
?>
1 php
2 $isRequired = true ;
3 if ( $isRequired )
4 {
5 function func( $op1 , $op2 )
6 {
7 return $op1 + $op2 ;
8 }
9 }
10 if ( $isRequired )
11 echo " func(1,3)= " . func( 1 , 3 );
12
13 function helloWorld()
14 {
15 return " Hello,world " ;
16 }
17 echo '
Call function helloWorld(): ' . helloWorld();
18 ?>
func( 1 , 3 ) = 4
Call function helloWorld() : Hello , world
1 php
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
11 /* We can't call subfunc() yet
12 since it doesn't exist. */
13
14 func();
15
16 /* Now we can call subfunc(),
17 func()'s processesing has
18 made it accessable. */
19
20 subfunc();
21
22 ?>
I don ' t exist until func() is called. I have alrady made
php
function MakeComputerBrand( $brand = " IBM " )
{
return " Making " . $brand . " computer now.
" ;
}
echo MakeComputerBrand();
echo MakeComputerBrand( " DELL " );
echo MakeComputerBrand( " HP " );
echo MakeComputerBrand( " Lenevo " );
?>
Making IBM computer now .
Making DELL computer now .
Making HP computer now .
Making Lenevo computer now .
php
function small_numbers()
{
return array ( 0 , 1 , 2 );
}
list ( $zero , $one , $two ) = small_numbers();
?>