PHPoC作为参数传达方式支持依据值的参数传递,依据参考的参数传递和基本参数值。
<?php
function func($arg1, $arg2) // pass by value
{
$temp = $arg1;
$arg1 = $arg2;
$arg2 = $temp;
return $arg1 + 1;
}
$var1 = 1;
$var2 = 2;
func($var1, $var2); // function call
echo "$var1, $var2"; // $var1 and $var2 are not swapped
?>
[result]
1, 2
<?php
function func(&$arg1, &$arg2) // pass by reference
{
$temp = $arg1;
$arg1 = $arg2;
$arg2 = $temp;
return $arg1 + 1;
}
$var1 = 1;
$var2 = 2;
func($var1, $var2); // function call
echo "$var1, $var2"; // $var1 and $var2 are swapped
?>
[result]
2, 1
<?php
function print_str($str = "Hello PHPoC!\r\n") // set default argument value
{
echo $str;
}
print_str(); // call print_str() without argument
?>
[result]
Hello PHPoC!