note 13222 modified in function.func-get-args by danbrown

From: Date: Sat, 19 Jul 2008 21:48:58 +0000
Subject: note 13222 modified in function.func-get-args by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-141768@lists.php.net to get a copy of this message
You can pass a variable number of arguments to a function whilst keeping references intact by using an array. The disadvantage of course, is that the called function needs to be aware that it's arguments are in an array. <?php // Prints "hello mutated world" function mutator($args=null) { $n=count($args); while($i<$n) $args[$i++] = "mutated"; } $a = "hello"; $b = "strange"; $c = "world"; mutator(array($a, &$b, $c)); echo "$a $b $c"; ?> --was-- You can pass a variable number of arguments to a function whilst keeping references intact by using an array. The disadvantage of course, is that the called function needs to be aware that it's arguments are in an array. <? // Prints "hello mutated world" function mutator($args=null) { $n=count($args); while($i<$n) $args[$i++] = "mutated"; } $a = "hello"; $b = "strange"; $c = "world"; mutator(array($a, &$b, $c)); echo "$a $b $c"; ?> http://php.net/manual/en/function.func-get-args.php

« previous php.notes (#141768) next »