Forum Moderators: coopster

Message Too Old, No Replies

The difference between various types of args

Wondering about what defines an argument

         

StupidScript

7:30 pm on Jan 27, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Here's a simple test function (testargs.php):

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'?

ramoneguru

10:53 pm on Jan 27, 2006 (gmt 0)

10+ Year Member



It looks like you're exploding on the '&' then concatenating the thing back together and sticking a ',' at the end.......Then your trying to pass that string into a function that appears to be expecting CSV's. Its not working cause you're passing in a string. Even though there is a comma in the string that's not how the argument is going to be parsed. It will just be interpreted as ONE long string w/ a comma in it. See it? (if not, its alright just keep asking for a more clear explination).

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

StupidScript

11:38 pm on Jan 27, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Thanks, 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'?

coopster

12:52 am on Jan 28, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Information may be passed to functions via the argument list, which is a comma-delimited list of expressions [php.net].

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.

coopster

2:22 am on Jan 28, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



I should have stated that you can dump a var to see what type it is. In the case of your failed QUERY_STRING example you would have noticed that the expression is a single expression of type string:
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);");