Home >Backend Development >PHP Tutorial >Detailed explanation of the difference between passing by value and passing by reference in PHP

Detailed explanation of the difference between passing by value and passing by reference in PHP

小云云
小云云Original
2018-03-14 11:11:342040browse

If a value is passed, if it is a non-object, a copy of the value will be passed. Any changes to this variable will not affect the original value. Passing a reference or object means passing the real memory address. Changes to this variable will affect the original value.

function f1($a) {//传值
  $a = $a + 1;
}
function f2(&$a) {//传引用
  $a = $a + 1;
}
$b = 1;
f1($b); 
echo $b; // 输出 1
$b = 1;
f2($b); 
echo $b; // 输出 2

Related recommendations:

What is the difference between value assignment and reference assignment in php?

The above is the detailed content of Detailed explanation of the difference between passing by value and passing by reference in PHP. 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