";"/> ";">
Home > Article > Backend Development > Detailed explanation of the usage of value transfer/reference transfer between PHP custom functions
php:Parameter transfer between functions
1. Value transfer
<?php function exam($var1){ $var1++; echo "In Exam:" . $var1 . "<br />"; } $var1 = 1; echo $var1 . "<br />"; exam($var1); echo $var1 . "<br />"; ?>
------------------------------------------------ -------------------------------
Output results:
1
In Exam: 2
1
---------------------------------------------- ----------------------------------
2.QuoteTransfer
<?php function exam( &$var1){ $var1++; echo "In Exam:" . $var1 . "<br />"; } $var1 = 1; echo $var1 . "<br />"; exam($var1); echo $var1 . "<br />"; ?>
----------------------------------------- ---------------------------------------------
Output result:
1
In Exam: 2
2
--------------------------------------------- ----------------------------------------
3.Yes Select parameter
function values($price, $tax=""){ $price += $prive * $tax; echo "Total Price:" . $price . "<br />"; } values(100, 0.25); values(100);
Output result:
Total Price: 125
Total Price: 100
------------------ -------------------------------------------------- -----------
4. If a object is passed in, the value of the object can be changed
(actually Variable$obj records the handle of this object. Passing $obj as a parameter can completely operate on the original object)
<?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 . "<br />"; } } 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
The above is the detailed content of Detailed explanation of the usage of value transfer/reference transfer between PHP custom functions. For more information, please follow other related articles on the PHP Chinese website!