php: Parameter passing between functions
1. Value passing
Copy code The code is as follows:
function exam($var1){
$var1++;
echo "In Exam:" . $var1 . "
";
}
$var1 = 1;
echo $var1 . "
";
exam($var1);
echo $var1 . "
?>
-------------------------------- -----------------------------------------------
Output result:
1
In Exam: 2
1
-------------------------------- --------------------------------------------------
2. Pass by reference
Copy code The code is as follows:
function exam( &$var1){
$var1++;
echo "In Exam:" . $var1 . "
";
}
$var1 = 1;
echo $var1 . "
";
exam($var1);
echo $var1 . "
";
?>
----------------------------------------- ----------------------------------------
Output result:
1
In Exam: 2
2
---------------------------------- -----------------------------------------------
3. Optional parameters
Copy code The code is as follows:
function values($price, $tax=" "){
$price += $prive * $tax;
echo "Total Price:" . $price . "
";
}
values( 100, 0.25);
values(100);
Output result:
Total Price: 125
Total Price: 100
-------- -------------------------------------------------- ---------------------
4. If an object is passed in, the value of the object can be changed ( In fact, the variable $obj records the handle of this object. If $obj is passed in as a parameter, the original object can be operated. )
Copy code The code is as follows:
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( );
?>
-------------------------------- -----------------------------------------------
Output Result:
Mr. zhan 12 male
Mr. zhan 13 male
Mr. zhan 14 male
http://www.bkjia.com/PHPjc/313595.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/313595.htmlTechArticlephp: Parameter passing between functions 1. Value passing copy code The code is as follows: ?php function exam($var1) { $var1++; echo "In Exam:" . $var1 . "br /"; } $var1 = 1; echo $var1 . "br /"; exam($v...