Home Backend Development PHP Tutorial php函数间的参数传递(值传递/引用传递)_PHP

php函数间的参数传递(值传递/引用传递)_PHP

Jun 01, 2016 am 11:59 AM
php function Parameter passing

php:函数间的参数传递

1.值传递
复制代码 代码如下:
function exam($var1){
$var1++;
echo "In Exam:" . $var1 . "
";
}

$var1 = 1;
echo $var1 . "
";
exam($var1);
echo $var1 . "
";
?>

-------------------------------------------------------------------------------
输出结果:
1
In Exam: 2
1
-------------------------------------------------------------------------------
2.引用传递
复制代码 代码如下:
function exam( &$var1){
$var1++;
echo "In Exam:" . $var1 . "
";
}

$var1 = 1;
echo $var1 . "
";
exam($var1);
echo $var1 . "
";
?>


-------------------------------------------------------------------------------
输出结果:
1
In Exam: 2
2
-------------------------------------------------------------------------------
3.可选参数
复制代码 代码如下:
function values($price, $tax=""){
$price += $prive * $tax;
echo "Total Price:" . $price . "
";
}

values(100, 0.25);
values(100);

输出结果:
Total Price: 125
Total Price: 100
-------------------------------------------------------------------------------
4.如果传入的是一个对象,可以更改该对象的值
(实际上变量$obj记录的是这个对象的句柄,将$obj作为参数传入,完全可以对原对象进行操作。)
复制代码 代码如下:
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 . "
";
}
}
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();
?>

-------------------------------------------------------------------------------
输出结果:
Mr. zhan 12 male
Mr. zhan 13 male
Mr. zhan 14 male

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

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Best practices for optimizing Golang function parameter passing performance Best practices for optimizing Golang function parameter passing performance Apr 13, 2024 am 11:15 AM

Best practices for optimizing Golang function parameter passing performance

Similarities and differences between PHP functions and Flutter functions Similarities and differences between PHP functions and Flutter functions Apr 24, 2024 pm 01:12 PM

Similarities and differences between PHP functions and Flutter functions

Comparing PHP functions to functions in other languages Comparing PHP functions to functions in other languages Apr 10, 2024 am 10:03 AM

Comparing PHP functions to functions in other languages

Summary of methods for implementing image editing and processing functions using PHP image processing functions Summary of methods for implementing image editing and processing functions using PHP image processing functions Nov 20, 2023 pm 12:31 PM

Summary of methods for implementing image editing and processing functions using PHP image processing functions

How performant are PHP functions? How performant are PHP functions? Apr 18, 2024 pm 06:45 PM

How performant are PHP functions?

Detailed explanation of C++ function parameters: Performance optimization of parameter passing in parallel programming Detailed explanation of C++ function parameters: Performance optimization of parameter passing in parallel programming Apr 27, 2024 pm 02:09 PM

Detailed explanation of C++ function parameters: Performance optimization of parameter passing in parallel programming

Research on parameter passing methods in Go language Research on parameter passing methods in Go language Apr 03, 2024 pm 02:48 PM

Research on parameter passing methods in Go language

C++ function pointer parameter passing mechanism C++ function pointer parameter passing mechanism Apr 19, 2024 pm 02:06 PM

C++ function pointer parameter passing mechanism

See all articles