Are strings referenced or copied, when passed as arguments or assigned to variables, in PHP5?
The debug_zval_dump() function might help you answer this question.
$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)
}
I get the following output :
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)
They are copies or dereferenced.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With