PHP5中是否引用了字符串?

PHP5中是否引用了字符串?,php,string,reference-counting,Php,String,Reference Counting,在PHP5中,字符串作为参数传递或分配给变量时是否被引用或复制?它们是副本或取消引用。该函数可能会帮助您回答此问题 例如,如果我运行以下代码部分: $str = 'test'; debug_zval_dump($str); // string(4) "test" refcount(2) my_function($str); debug_zval_dump($str); // string(4) "test" refcount(2) function my_functio

在PHP5中,字符串作为参数传递或分配给变量时是否被引用或复制?

它们是副本或取消引用。

该函数可能会帮助您回答此问题


例如,如果我运行以下代码部分:

$str = 'test';
debug_zval_dump($str);      // string(4) "test" refcount(2)

my_function($str);
debug_zval_dump($str);      // string(4) "test" refcount(2)

function my_function($a) {
    debug_zval_dump($a);    // string(4) "test" refcount(4)
    $plop = $a . 'glop';
    debug_zval_dump($a);    // string(4) "test" refcount(4)
    $a = 'boom';
    debug_zval_dump($a);    // string(4) "boom" refcount(2)
}
我得到以下输出:

string(4) "test" refcount(2)
string(4) "test" refcount(4)
string(4) "test" refcount(4)
string(4) "boom" refcount(2)
string(4) "test" refcount(2)

因此,我想说:

  • 字符串在传递给函数时(可能在分配给变量时)为“refcounted”
  • 但别忘了PHP确实是写时拷贝的

有关更多信息,以下是一些可能有用的链接:

  • (法语)

我一直知道我的法语知识迟早会派上用场的。我知道这里周围有些人懂法语,所以我想“为什么不发呢?”;-)