php函数间的参数传递(值传递/引用传递)_php技巧
程序员文章站
2024-01-04 10:20:22
...
php:函数间的参数传递
1.值传递
function exam($var1){
$var1++;
echo "In Exam:" . $var1 . "
";
}
$var1 = 1;
echo $var1 . "
";
exam($var1);
echo $var1 . "
";
?>
-------------------------------------------------------------------------------
输出结果:
1
In Exam: 2
1
-------------------------------------------------------------------------------
2.引用传递
function exam( &$var1){
$var1++;
echo "In Exam:" . $var1 . "
";
}
$var1 = 1;
echo $var1 . "
";
exam($var1);
echo $var1 . "
";
?>
-------------------------------------------------------------------------------
输出结果:
1
In Exam: 2
2
-------------------------------------------------------------------------------
3.可选参数
function values($price, $tax=""){
$price += $prive * $tax;
echo "Total Price:" . $price . "
";
}
values(100, 0.25);
values(100);
输出结果:
Total Price: 125
Total Price: 100
-------------------------------------------------------------------------------
4.如果传入的是一个对象,可以更改该对象的值
(实际上变量$obj记录的是这个对象的句柄,将$obj作为参数传入,完全可以对原对象进行操作。)
class Obj{
public $name;
public $age;
public $gander;
public function __construct($name, $age, $gander){
$this->name = $name;
$this->age = $age;
$this->gander = $gander;
}
public function show_info(){
echo $this->name . " " . $this->age . " " . $this->gander . "
";
}
}
function grow($obj){
$obj->age++;
}
function test(){
$obj = new Obj("Mr. zhan", "12", "male");
$obj->show_info();
grow($obj);
$obj->show_info();
grow($obj);
$obj->show_info();
}
test();
?>
-------------------------------------------------------------------------------
输出结果:
Mr. zhan 12 male
Mr. zhan 13 male
Mr. zhan 14 male
1.值传递
复制代码 代码如下:
function exam($var1){
$var1++;
echo "In Exam:" . $var1 . "
";
}
$var1 = 1;
echo $var1 . "
";
exam($var1);
echo $var1 . "
";
?>
-------------------------------------------------------------------------------
输出结果:
1
In Exam: 2
1
-------------------------------------------------------------------------------
2.引用传递
复制代码 代码如下:
function exam( &$var1){
$var1++;
echo "In Exam:" . $var1 . "
";
}
$var1 = 1;
echo $var1 . "
";
exam($var1);
echo $var1 . "
";
?>
-------------------------------------------------------------------------------
输出结果:
1
In Exam: 2
2
-------------------------------------------------------------------------------
3.可选参数
复制代码 代码如下:
function values($price, $tax=""){
$price += $prive * $tax;
echo "Total Price:" . $price . "
";
}
values(100, 0.25);
values(100);
输出结果:
Total Price: 125
Total Price: 100
-------------------------------------------------------------------------------
4.如果传入的是一个对象,可以更改该对象的值
(实际上变量$obj记录的是这个对象的句柄,将$obj作为参数传入,完全可以对原对象进行操作。)
复制代码 代码如下:
class Obj{
public $name;
public $age;
public $gander;
public function __construct($name, $age, $gander){
$this->name = $name;
$this->age = $age;
$this->gander = $gander;
}
public function show_info(){
echo $this->name . " " . $this->age . " " . $this->gander . "
";
}
}
function grow($obj){
$obj->age++;
}
function test(){
$obj = new Obj("Mr. zhan", "12", "male");
$obj->show_info();
grow($obj);
$obj->show_info();
grow($obj);
$obj->show_info();
}
test();
?>
-------------------------------------------------------------------------------
输出结果:
Mr. zhan 12 male
Mr. zhan 13 male
Mr. zhan 14 male
推荐阅读
-
php函数间的参数传递(值传递/引用传递)_php技巧
-
怎么隐藏php地址的php后辍,还有让传递的参数人性化
-
请教小弟我写的这个php函数体传递变量的时候为什么总是上一个函数体的值
-
php获取jquery ajax传递的值,在php中如何把这个值设置为全局变量
-
函数参数传递、数组指针、二级指针、左值、引用
-
php 引用只允许定义在函数参数中,实时传递引用是禁止的
-
引用传递 - php的引用不是指针到底是什么,看看这个输出结果
-
More Effective C++----(12)理解"抛出一个异常"与"传递一个参数"或"调用一个虚函数"间的差异
-
PHP里的值传递与引用传递
-
Java的参数传递是「按值传递」还是「按引用传递」?