php: Parameter passing between functions
1. Value passing
The code is as follows:
<?php
function Exam ($var 1) {
$var 1++;
echo "in Exam:". $var 1. "<br/>";
}
$var 1 = 1;
echo $var 1. "<br/>";
Exam ($var 1);
echo $var 1. "<br/>";
?>
-------------------------------------------------------------------------------
Output Result:
1
In Exam:2
1
-------------------------------------------------------------------------------
2. Reference Delivery
The code is as follows:
<?php
Function Exam (& $var 1) {
$var 1++;
echo "in Exam:". $var 1. "<br/>";
}
$var 1 = 1;
echo $var 1. "<br/>";
Exam ($var 1);
echo $var 1. "<br/>";
?>
-------------------------------------------------------------------------------
Output Result:
1
In Exam:2
2
-------------------------------------------------------------------------------
3. Optional Parameters
The code is as follows:
function values ($price, $tax = "") {
$price + = $price * $tax;
echo "Total Price:". $price. "<br/>";
}
VALUES (100, 0.25);
VALUES (100);
Output Result:
Total price:125
Total price:100
-------------------------------------------------------------------------------
4. If an object is passed in, you can change the value of the object
(In fact, the variable $obj records the handle of this object, passing the $obj as a parameter, it is completely possible to manipulate the original object.) )
The code is as follows:
<?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", "n", "male");
$obj->show_info ();
Grow ($obj);
$obj->show_info ();
Grow ($obj);
$obj->show_info ();
}
Test ();
?>
-------------------------------------------------------------------------------
Output Result:
Mr. Zhan Male
Mr. Zhan Male
Mr. Zhan Male
Parameter passing between PHP functions (value passing/reference passing)