|
|
 |
在PHP3中,您也可以定義一些函數,定義的方式大概如下:
function foo( $arg_1, $arg_2, ..., $arg_n ) {
echo "Example function.\n";
return $retval;
}
- 傳回值
-
如果在函數中要傳回值,可以使用 Return
範例1:傳回單一值
function my_sqrt( $num ) {
return $num * $num;
}
echo my_sqrt( 4 ); // outputs '16'.
範例2:傳回多重值
function foo() {
return array( 0, 1, 2 );
}
list( $zero, $one, $two ) = foo();
- 函數參數
-
在程式中,您也可以傳遞參數至函數中處理,參數傳遞又分為 Passing by reference 及 Passing by value,預設值為 Passing
by value。
範例1:Passing by reference
function foo( &$bar ) {
$bar .= ' and something extra.';
}
$str = 'This is a string, ';
foo2( $str );
echo $str; // outputs 'This is a string, and something extra.'
範例2:Passing by value
function foo( $bar ) {
$bar .= ' and something extra.';
}
$str = 'This is a string, ';
foo2( $str );
echo $str; // outputs 'This is a string, '
foo2( &$str );
echo $str; // outputs 'This is a string, and something extra.'
|
|
|
Copyright
2002 SYSCOM Computer Engineering Co. All rights reserved.
|
|
|