php 函数传值_传址_函数参数,php 函数传值,传址,函数参数

函数参数传递的方式有两种:

1 、传值方式。缺省情况下,函数参数通过传值的方式传递,因此即使在函数内部改变参数的值,它并不会改变函数外部参数的值。

2 、传址方式。传址时只需在函数调用时在参数的前面加上“&”号即可。将函数外部的值的内存地址传递给内部的参数,在函数内部的所有操作都会改变函数外部参数的值。所以希望函数修改外部参数的值,必须使用传址传址方式。

//定义一个函数

function f3 ( $a ){

$a ++;

}

$x = 1 ;

f3 ( $x ) ;

echo " x= $x " ; //传值方式调用函数

$x = 1 ;

f3 ( & $x ) ;

echo " x= $x " ; //传址方式调用函数

?>

function add_some_extra ( & $string )

{

$string .= " and something extra. " ;

}

$str = " This is a string, " ;

add_some_extra ( $str ) ;

echo $str ; // 输出"This is a string, and something extra."

?>

/* 在PHP中,函数不需要在被调用之前定义,在调用后才进行定义也是允许的。

在少数情况下,函数在需要一定的判断条件下,才能被定义。这样函数的定义必须在函数被调用之前完成。 */

$makefoo = true ;

bar () ; /*你不能调用foo()函数,它在这里不存在。但是能够调用bar(),调用之后在后面进行定义即可。*/

if ( $makefoo ) {

function foo ()

{

echo " foofoo " ;

}

}

if ( $makefoo ) foo () ; /* 现在我们可以正常调用foo(),因为只有$makefoo为true和定义了foo()函数后,foo()函数才存在。 */

function bar ()

{

echo " barbar " ;

}

?>

function foo ()

{

function bar ()

{

echo " I don"t exist until foo() is called. " ;

}

}

/* 这里不能调用bar(),因为它不存在。 */

foo () ;

/* 现在我们可以调用bar(),只有在调用foo()后,bar()才存在。 */

bar () ;

?>

为函数指定默认参数的值

function test_defaultargs ( $arg = " default value " ){

echo " 参数值为: " . $arg . "
" ;

}

test_defaultargs () ;

test_defaultargs ( " new value " ) ;

?>

function makecoffee ( $type = " cappuccino " )

{

return " Making a cup of $type . " ;

}

echo makecoffee () ;

echo makecoffee ( " espresso " ) ;

?>

请注意当使用默认参数时,任何默认参数必须放在任何非默认参数的右侧;否则,可能函数将不会按照预期的情况运行。

function makeyogurt ( $type = " acidophilus " , $flavour )

{

return " Making a bowl of $type $flavour . " ;

}

echo makeyogurt ( " raspberry " ) ; // 这个例子将不会按照我们预期的情况运行。

?>

function makeyogurt ( $flavour , $type = " acidophilus " )

{

return " Making a bowl of $type $flavour . " ;

}

echo makeyogurt ( " raspberry " ) ; // 这个例子的输出是:Making a bowl of acidophilus raspberry.

?>

函数名可变

function f1 (){

echo " 这是函数f1()。
" ;

}

function f2 (){

echo " 这是函数f2()。
" ;

}

$var1 = " f1 " ;

$var1 () ; //调用函数f1()

$var1 = " f2 " ;

$var1 () ; //调用函数f2()

//注意:调用可变函数名需要在变量前加$。

?>

function foo () {

echo " foofoo.
" ;

}

function bar ( $arg = "" ) {

echo " barbar" $arg ".
" ;

}

function echoit ( $string )

{

echo $string ;

}

$func = " foo " ;

$func () ; // 调用foo()

$func = " bar " ;

$func ( " test " ) ; // 调用bar()

$func = " echoit " ;

$func ( " test " ) ; // 调用echoit()

?>

函数可变长度参数

//向函数传递数组

function takes_array ( $input )

{

echo " $input [0] + $input [1] = " , $input [ 0 ] + $input [ 1 ] ;

}

?>

func_num_args() -- 返回传递给函数的参数的数量

function foo ()

{

$numargs = func_num_args () ;

echo " Number of arguments: $numargs ."
" " ;

}

foo ( 1 , 2 , 3 ) ;

?>

func_get_arg() -- 从参数列表中返回一个参数值

function foo ()

{

$numargs = func_num_args () ;

echo " Number of arguments: $numargs ."
" " ;

if ( $numargs >= 2 ) {

echo " Second argument is: " . func_get_arg ( 1 ) . "
" ;

}

}

foo ( 1 , 2 , 3 ) ;

?>

func_get_args() -- 返回一个包含函数参数的数组

function foo ()

{

$numargs = func_num_args () ;

echo " Number of arguments: $numargs ."
" " ;

if ( $numargs >= 2 ) {

echo " Second argument is: " . func_get_arg ( 1 ) . "
" ;

}

$arg_list = func_get_args () ;

for ( $i = 0 ; $i < $numargs ; $i ++ ) {

echo " Argument $i is: " . $arg_list [ $i ] . "
" ;

}

}

foo ( 1 , 2 , 3 ) ;

?>

原文出自【比特网】,转载请保留原文链接:http://bbs.chinabyte.com/thread-350557-1-1.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值