Parameter passing between PHP functions (value passing/reference passing)

Source: Internet
Author: User

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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.