PHP函数中参数按值传递与按引用传递的区别
Alright, let's break this down clearly—since mixing up variables, references, and parameter passing is a super common gotcha in PHP, especially for folks moving from other languages.
First, let's get the core definitions straight:
普通参数变量: Think of this as a container that holds a value. When you assign one variable to another (
$b = $a), PHP creates a copy of the value in$aand stores it in$b. These two variables live in separate memory spaces, so modifying one doesn't affect the other.$a = 10; $b = $a; $b = 20; echo $a; // Outputs 10—$b's change doesn't touch $a引用: A reference is just an alias for an existing variable. Instead of copying a value,
$b = &$amakes$bpoint to the exact same memory address as$a. Any change to either variable directly modifies the underlying value they both share.$a = 10; $b = &$a; $b = 20; echo $a; // Outputs 20—$b and $a are two names for the same value
Now let's apply this to function parameters, which is where most people run into confusion.
按值传递(默认行为)
When you pass a variable to a function normally, PHP creates a copy of that variable for the function to use. Changes made to the parameter inside the function stay inside the function—they don't affect the original variable outside.
Example:
function addFive($num) { $num += 5; echo "Inside function: $num\n"; // Outputs 15 } $original = 10; addFive($original); echo "Outside function: $original\n"; // Outputs 10—original is untouched
This is great for cases where you don't want to alter the original data—like calculating a result based on a value without modifying the source.
按引用传递
To make a function modify the original variable, you need to pass it by reference. Do this by adding an & before the parameter name in the function definition.
Now, the function works directly with the original variable's memory address—no copy is made, and any changes inside the function will reflect outside.
Example:
function addFiveByRef(&$num) { $num += 5; echo "Inside function: $num\n"; // Outputs 15 } $original = 10; addFiveByRef($original); echo "Outside function: $original\n"; // Outputs 15—original is changed
Key differences to remember
Performance impact: For large data (like huge arrays), pass-by-value can be expensive because PHP has to copy the entire dataset. Pass-by-reference avoids this copy, making it faster and more memory-efficient for big inputs.
// Passing a massive array by value copies all 100k elements—slow! function processBigArray($arr) { /* ... */ } processBigArray(range(1, 100000)); // Passing by reference uses the original array directly—no copy function processBigArrayByRef(&$arr) { /* ... */ } processBigArrayByRef(range(1, 100000));Literal values aren't allowed: You can't pass a literal (like
10or"hello") to a reference parameter—PHP will throw a fatal error. You have to pass a variable.// ❌ Error: Can't pass literal to reference parameter addFiveByRef(10); // ✅ Works: Pass a variable $myNum = 10; addFiveByRef($myNum);Copy-on-Write optimization: PHP has a smart trick for pass-by-value called "Copy-on-Write" (COW). When you assign a variable to another or pass it to a function, PHP doesn't immediately copy the value—it only copies it if you modify the new variable. This reduces unnecessary memory usage for pass-by-value scenarios.
内容的提问来源于stack exchange,提问作者Saad

