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

小云云
Release: 2023-03-21 11:30:01
Original
1941 people have browsed it

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

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!

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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!