Jump to content

functions with multiple paramters...


blueman378

Recommended Posts

Hi guys,

 

Im wondering i have a function which draws some stuff, anyway it takes 5 paramaters but the only one that changes when i call it in one place (over 20 times) is the text.

 

So im wondering is there any way to say

 

params = "param value 2, param value 3, param value 4, param value 5";

text = "text1";

function_name(text, params);

 

instead of repeating all the info over and over, the function itself cannot be edited.

 

i tried doing basically what i said above except it treats it as a single paramter, is there any way to make it treat it as all the params?

Link to comment
https://forums.phpfreaks.com/topic/177617-functions-with-multiple-paramters/
Share on other sites

you can use eval. something like

params = "param value 2, param value 3, param value 4, param value 5";
text = "text1"; 
eval("function_name(".$text.", ".$params.")");

didnt test this though.

 

and eval is not the best way to go. It will be a kind of shortcut for what you want to do, but eval is much slower than just normally parsing php, and since you are more or less being lazy there really isn't any substantial reason to use eval in this case. why can't you alter the function?

the cons of using eval far out weight any pros you get from using eval. If you would be using eval alot, you will see a hige decrease in preformance. It may be ok if you are going to be doing a couple of times, but any more than 5 and I would stay away from it. also, if any user input ever goes into eval, you have to make sure you sanitze it as well as you can

You could of course use the func_get_args() to get all the arguments of the function. That way you don't actually need to specify them at all, and just let your function handle them

Take a look at a function I created here that combines mysql_real_escape_string and the sprintf function

http://www.jaygilford.com/php/sprintf-and-mysql_real_escape_string-all-in-one-function/

You could of course use the func_get_args() to get all the arguments of the function. That way you don't actually need to specify them at all, and just let your function handle them

Take a look at a function I created here that combines mysql_real_escape_string and the sprintf function

http://www.jaygilford.com/php/sprintf-and-mysql_real_escape_string-all-in-one-function/

 

the OP doesn't have access to the function definition.

Oh, oops! My bad lol. Didn't realise that

 

I would highly recommend against using eval for it also, as you don't want to be messing with stuff like that

 

Surely you could use call_user_func in it's place

 

Yeah, call_user_func_array has already been suggested.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.