PHP How to know if a variable is a reference?
php, reference
Solution
No, the issue is that the first parameter of the function is pass-by-reference (meaning the function can modify the argument in the caller's scope). Therefore, you must pass a variable or something which is assignable as the first argument. When you create the array like `array($a)`, it just copies the value of the variable `$a` (which is 0) into a slot in the array. It does not refer back to the variable `$a` in any way. And then when you call the function, it is as if you're doing this, which does not work:
test(0)
If you really wanted to, you could put `$a` into the array by reference, but it's kinda tricky:
<?php
$a = 0;
$args = array(&$a);
function test(&$a) {
$a++;
}
call_user_func_array('test', $args);
?>
As to how you would tell, that the array element is a reference... that is hard. You could do `var_dump()` on the array, and search for the "&" symbol:
> var_dump($args);
array(1) {
[0]=>
&int(1)
}
Problem
I want to call a function with call_user_func_array but i noticed that if an argument is a reference in the function definition and is a simple value in call_user_func_array, the following warning appears: Warning: Parameter 1 to test() expected to be a reference, value given Here is a simple example of what i am trying to do: ``` <?php $a = 0; $args = array($a); function test(&$a) { $a++; } $a = 0; call_user_func_array('test', $args); ?> ``` My question is: how can i know if a value (in this case the first value of $args) is a reference or not ?