You need to create a function in which you can pass the variable the number of arguments by reference.

The task is as follows: I pass any number of numeric arguments to a function, and the function changes their values, for example, to +5;

Those.

function W() { for($i = 0; $i < func_num_args(); $i++) // Может как то тут можно получить ссылку на куждый аргумент и изменить его значеие? } 
  • Apparently, no way to do so. Transfer an array with values ​​by reference (array values ​​can also be assigned by reference, if necessary). - etki
  • And how then? function W (& $ arr) {for ($ i = 0; $ i <count ($ arr); $ i ++) $ arr [i] + = 5; } $ a = 1; $ b = 3; $ c = 5; W (array ($ a, $ b, $ c)); Did you mean that? - S9ZAAAA
  • @ Hide2 function increment (& $ data) {foreach ($ data as & $ item) {$ item + = 5; }} $ data = array (1,5,6,18); increment ($ data); var_dump ($ data); // array (4) {[0] => int (6) [1] => int (10) [2] => int (11) [3] => int (23)} - etki
  • 2
    it is not necessary to pass arguments to the function by reference, but now it is also an error. A link or not a link is now defined by a function declaration Note: In the function call, there is no reference sign - it is only in the function definition. This is enough to correctly pass the arguments by reference. Beginning with PHP 5.3.0, you can get a warning that passing a variable by reference is outdated if you use & in foo (& $ a) ;. Since PHP 5.4.0, passing a variable by reference has become impossible, so using this technique will result in a fatal error. - zb '

1 answer 1

To access the function arguments you need to use func_get_args.

Here it is written in great detail with examples:

http://ru2.php.net/manual/ru/function.func-get-args.php

  function W() { $arg_list = func_get_args(); foreach ($arg_list as $key => $value) { $arg_list[$key] += 5; } return $arg_list; } 

I did not notice that the values ​​are transmitted by reference, then this code will not work

  • func_get_args seems to only return the values ​​of the arguments, but does not have access to them, IMHO - S9ZAAAA
  • You say that you need to change the values ​​of the arguments to 5. So with the help of func_get_args you can do it - hetfield