Forum Moderators: coopster
function getArg() { $arg=func_num_args(); $arglist=func_get_args(); echo "Number of arguments: $arg <br />\n"; for ($i=0;$i<$args;$i++) { echo "Argument $i : ".$arglist[$i]."<br />\n"; } } Here's a simple test of the function:
getArg(1,2,3); Returns:
Number of arguments: 3 Argument 0 : 1 Argument 1 : 2 Argument 2 : 3 So far, so good.
Here's a test URI for the page to munch on:
testargs.php?arg1=something&arg2=nothing Here's a test query parsing part:
$newargs=explode("&",$_SERVER['QUERY_STRING']); for($i=0;$i<sizeof($newargs);$i++) { $vals.=$newargs[$i].","; } $vals=substr($vals,0,-1); Here's the test with the new string:
getArg($vals); Returns:
Number of arguments: 1 Argument 0 : arg1=something,arg2=nothing Hmmm. Now why is that? I've tried defining the elements of the second test with quotes and apostrophes with no difference.
What makes an 'argument'?
I used arrays to get around this, now that you know the problem try and get a diff solution :-)
<?php
//Cut & patste, this will work...
//Preconditions: $some must be an array of elements greater than 0. Not my falut if it blows up cause you passed in 0 arguments :P
function getArg($some) {
$arg = count($some);
echo "Number of arguments: $arg <br />\n";
for ($i=0;$i<$arg;$i++) {
echo "Argument $i : ".$some[$i]."<br />\n";
}
}
$temp = array(1,2,3);
getArg($temp);
$newargs=explode("&",$_SERVER['QUERY_STRING']);
print_r($newargs);
$size = count($newargs);
print "Size of array is $size <br/>";
getArg($newargs);
?>
--Nick
Actually, I have many other ways to get the values, now including one more from you! :)
My point is probably obscured by my examples. My main question is what are
func_num_args() and func_get_args() keying off of to separate the arguments? function getArg() { $arg=func_num_args(); $arglist=func_get_args(); echo "Number of arguments: $arg <br />\n"; for ($i=0;$i<$arg;$i++) { echo "Argument $i : ".$arglist[$i]."<br />\n"; } } $temp=array(1,2,3); getArg($temp); returns
Number of arguments: 1 Argument 0 : Array The two functions clearly don't read into the array. Fair enough ... it's a declared construct.
So, how are
func_num_args() and func_get_args() defining an 'argument'? Why does
getArg(1,2,3) pass separate, recognizable arguments when that same 1,2,3 that has been concatenated from separate sources or arranged in an array is interpreted as one argument? getArg(1,2,3) = works $temp="1,2,3", getArg($temp) = doesn't work $temp=array(1,2,3), getArg($temp) = doesn't work What defines an 'argument'?
Resource:
[php.net...]
Strings and arrays are themselves each a single expression. The examples shown would have to be formatted as a comma-delimited list in order to be passed as separate expressions.
Yes, it is a bit to wrap your head around but this ...
getArg(1,2,3)
is quite different than this ...
getArg(array(1,2,3))
The first is a comma-separated list while the second is a single expression which happens to be an array.
var_dump($vals);
// prints: string(27) "arg1=something,arg2=nothing"
On a side note, if you are trying to build a dynamic list and fire that off to your function, you can indeed do so. This is where PHP's eval() function comes in quite handy. For example, using your demo:
$temp='1,2,3';
eval("getArg($temp);");
As far as an array goes, you will want to flatten it out first:
$temp=array(1,2,3);
$temp = implode(',', $temp);
eval("getArg($temp);");