faqts : Computers : Programming : Languages : PHP : kms : Functions

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

9 of 13 people (69%) answered Yes
Recently 7 of 10 people (70%) answered Yes

Entry

Can I pass arguments to a function as a single string?

Jun 11th, 1999 07:00
Nathan Wallace, Nathan Wallace, Adam Trachtenberg


I have a string which contains all of the arguments for a function
separated by commas.

$args = "$foo, $bar";   or   $args = "1, 2";

Can I just replace the arguments in the function call with this string?

test($args);

No!

If you do this $args is treated as a single string argument to the
function.  This argument just happens to contain the correct arguments,
but the function call doesn't know or care that this is the case.

So you will get weird behaviour if you try to do this since the function
call will not have the correct number of arguments and the single
argument passed will probably contain invalid data.

If you already have $args set as above, then try doing this:

list($foo, $bar) = explode(', ',$args);

test($foo, $bar);

Note that it is important whether you have spaces between your arguments
in $args.  If you don't have spaces, then you should use ',' in the
explode statement instead of ', ' which was used in the example above.