函數(shù)參數(shù)傳遞的方式有兩種:
1 、傳值方式。缺省情況下,函數(shù)參數(shù)通過傳值的方式傳遞,因此即使在函數(shù)內部改變參數(shù)的值,它并不會改變函數(shù)外部參數(shù)的值。
2 、傳址方式。傳址時只需在函數(shù)調用時在參數(shù)的前面加上“&”號即可。將函數(shù)外部的值的內存地址傳遞給內部的參數(shù),在函數(shù)內部的所有操作都會改變函數(shù)外部參數(shù)的值。所以希望函數(shù)修改外部參數(shù)的值,必須使用傳址傳址方式。
<?php
//定義一個函數(shù)
function f3 ( $a ){
$a ++;
}
$x = 1 ;
f3 ( $x ) ;
echo " x= $x " ; //傳值方式調用函數(shù)
$x = 1 ;
f3 ( & $x ) ;
echo " x= $x " ; //傳址方式調用函數(shù)
?>
<?php
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
/* 在PHP中,函數(shù)不需要在被調用之前定義,在調用后才進行定義也是允許的。
在少數(shù)情況下,函數(shù)在需要一定的判斷條件下,才能被定義。這樣函數(shù)的定義必須在函數(shù)被調用之前完成。 */
$makefoo = true ;
bar () ; /*你不能調用foo()函數(shù),它在這里不存在。但是能夠調用bar(),調用之后在后面進行定義即可。*/
if ( $makefoo ) {
function foo ()
{
echo " foofoo " ;
}
}
if ( $makefoo ) foo () ; /* 現(xiàn)在我們可以正常調用foo(),因為只有$makefoo為true和定義了foo()函數(shù)后,foo()函數(shù)才存在。 */
function bar ()
{
echo " barbar " ;
}
?>
<?php
function foo ()
{
function bar ()
{
echo " I don't exist until foo() is called. " ;
}
}
/* 這里不能調用bar(),因為它不存在。 */
foo () ;
/* 現(xiàn)在我們可以調用bar(),只有在調用foo()后,bar()才存在。 */
bar () ;
?>
為函數(shù)指定默認參數(shù)的值
<?php
function test_defaultargs ( $arg = " default value " ){
echo " 參數(shù)值為: " . $arg . " <br> " ;
}
test_defaultargs () ;
test_defaultargs ( " new value " ) ;
?>
<?php
function makecoffee ( $type = " cappuccino " )
{
return " Making a cup of $type . " ;
}
echo makecoffee () ;
echo makecoffee ( " espresso " ) ;
?>
請注意當使用默認參數(shù)時,任何默認參數(shù)必須放在任何非默認參數(shù)的右側;否則,可能函數(shù)將不會按照預期的情況運行。
<?php
function makeyogurt ( $type = " acidophilus " , $flavour )
{
return " Making a bowl of $type $flavour . " ;
}
echo makeyogurt ( " raspberry " ) ; // 這個例子將不會按照我們預期的情況運行。
?>
<?php
function makeyogurt ( $flavour , $type = " acidophilus " )
{
return " Making a bowl of $type $flavour . " ;
}
echo makeyogurt ( " raspberry " ) ; // 這個例子的輸出是:Making a bowl of acidophilus raspberry.
?>
函數(shù)名可變
<?php
function f1 (){
echo " 這是函數(shù)f1()。<br> " ;
}
function f2 (){
echo " 這是函數(shù)f2()。<br> " ;
}
$var1 = " f1 " ;
$var1 () ; //調用函數(shù)f1()
$var1 = " f2 " ;
$var1 () ; //調用函數(shù)f2()
//注意:調用可變函數(shù)名需要在變量前加$。
?>
<?php
function foo () {
echo " foofoo.<br> " ;
}
function bar ( $arg = '' ) {
echo " barbar' $arg '.<br> " ;
}
function echoit ( $string )
{
echo $string ;
}
$func = ' foo ' ;
$func () ; // 調用foo()
$func = ' bar ' ;
$func ( ' test ' ) ; // 調用bar()
$func = ' echoit ' ;
$func ( ' test ' ) ; // 調用echoit()
?>
函數(shù)可變長度參數(shù)
<?php
//向函數(shù)傳遞數(shù)組
function takes_array ( $input )
{
echo " $input [0] + $input [1] = " , $input [ 0 ] + $input [ 1 ] ;
}
?>
func_num_args() -- 返回傳遞給函數(shù)的參數(shù)的數(shù)量
<?php
function foo ()
{
$numargs = func_num_args () ;
echo " Number of arguments: $numargs .'<br>' " ;
}
foo ( 1 , 2 , 3 ) ;
?>
func_get_arg() -- 從參數(shù)列表中返回一個參數(shù)值
<?php
function foo ()
{
$numargs = func_num_args () ;
echo " Number of arguments: $numargs .'<br>' " ;
if ( $numargs >= 2 ) {
echo " Second argument is: " . func_get_arg ( 1 ) . " <br> " ;
}
}
foo ( 1 , 2 , 3 ) ;
?>
func_get_args() -- 返回一個包含函數(shù)參數(shù)的數(shù)組
<?php
function foo ()
{
$numargs = func_num_args () ;
echo " Number of arguments: $numargs .'<br>' " ;
if ( $numargs >= 2 ) {
echo " Second argument is: " . func_get_arg ( 1 ) . " <br> " ;
}
$arg_list = func_get_args () ;
for ( $i = 0 ; $i < $numargs ; $i ++ ) {
echo " Argument $i is: " . $arg_list [ $i ] . " <br> " ;
}
}
foo ( 1 , 2 , 3 ) ;
?>
本站僅提供存儲服務,所有內容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權內容,請
點擊舉報。