variadic functions - pass variable number of arguments (both key & value) in PHP -
how can pass variable number of argument(both key , values) php
<?php function tryfun() { echo "inside try()"; $args = func_get_args(); print_r($args); } tryfun(1,2); tryfun(array("key"=>"value")); ?>
output
root@ip-:~# php trycmd.php inside try()array ( [0] => 1 [1] => 2 ) inside try()array ( [0] => array ( [key] => value ) )
what want be
root@ip:~# php trycmd.php inside try()array ( [0] => 1 [1] => 2 ) inside try()array ( [key] => value )
how can achieve in php using debian 7 wheezy php 5.4
you cannot dictate "names" of arguments outside function.
function ($foo, $bar) ...
here first positional argument assigned variable $foo
, second 1 $bar
.
function () { $args = func_get_args(); ... }
here positionally passed arguments assigned array $args
. values have no "name", because "names" aren't passed.
if pass array key-value pairs, do, argument array contains key-value pairs. array still plays according above rules.
maybe you're looking extract()
, or maybe want not how works.
Comments
Post a Comment