";"/> ";">

Home  >  Article  >  Backend Development  >  Detailed explanation of the usage of value transfer/reference transfer between PHP custom functions

Detailed explanation of the usage of value transfer/reference transfer between PHP custom functions

伊谢尔伦
伊谢尔伦Original
2017-06-26 09:33:151168browse

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn