You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

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.

参数变量 vs 引用:基础概念

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 $a and 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 = &$a makes $b point 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
    
函数中按值传递 vs 按引用传递:具体差异

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

  1. 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));
    
  2. Literal values aren't allowed: You can't pass a literal (like 10 or "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);
    
  3. 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.20 12:22:46