php函数间的参数传递(值传递/引用传递)

PHP:函数间的参数传递

1.值传递


<div class="codetitle"><a style="CURSOR: pointer" data="9867" class="copybut" id="copybut9867" onclick="doCopy('code9867')"> 代码如下:
<div class="codebody" id="code9867">
<?PHP
function exam($var1){
$var1++;
echo "In Exam:" . $var1 . "
";
} $var1 = 1;
echo $var1 . "
";
exam($var1);
echo $var1 . "
";
?>

-------------------------------------------------------------------------------
输出结果:
1
In Exam: 2
1
-------------------------------------------------------------------------------

2.引用传递


<div class="codetitle"><a style="CURSOR: pointer" data="1581" class="copybut" id="copybut1581" onclick="doCopy('code1581')"> 代码如下:
<div class="codebody" id="code1581">
<?PHP
function exam( &$var1){
$var1++;
echo "In Exam:" . $var1 . "
";
} $var1 = 1;
echo $var1 . "
";
exam($var1);
echo $var1 . "
";
?>
-------------------------------------------------------------------------------
输出结果:
1
In Exam: 2
2
-------------------------------------------------------------------------------

3.可选参数


<div class="codetitle"><a style="CURSOR: pointer" data="79555" class="copybut" id="copybut79555" onclick="doCopy('code79555')"> 代码如下:
<div class="codebody" id="code79555">
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作为参数传入,完全可以对原对象进行操作。)
<div class="codetitle"><a style="CURSOR: pointer" data="19076" class="copybut" id="copybut19076" onclick="doCopy('code19076')"> 代码如下:<div class="codebody" id="code19076">
<?PHP
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

参数传递

相关文章

Hessian开源的远程通讯,采用二进制 RPC的协议,基于 HTTP 传输。可以实现PHP调用Java,Python,C#等多语...
初识Mongodb的一些总结,在Mac Os X下真实搭建mongodb环境,以及分享个Mongodb管理工具,学习期间一些总结...
边看边操作,这样才能记得牢,实践是检验真理的唯一标准.光看不练假把式,光练不看傻把式,边看边练真把式....
在php中,结果输出一共有两种方式:echo和print,下面将对两种方式做一个比较。 echo与print的区别: (...
在安装好wampServer后,一直没有使用phpMyAdmin,今天用了一下,phpMyAdmin显示错误:The mbstring exte...
变量是用于存储数据的容器,与代数相似,可以给变量赋予某个确定的值(例如:$x=3)或者是赋予其它的变...