Home > Backend Development > PHP Tutorial > 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

伊谢尔伦
Release: 2023-03-11 13:00:01
Original
1191 people have browsed it

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 />"; 
?>
Copy after login

------------------------------------------------ -------------------------------
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 />"; 
?>
Copy after login

----------------------------------------- ---------------------------------------------
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);
Copy after login

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(); 
?>
Copy after login

----------- -------------------------------------------------- ------------------
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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template