asmith Posted August 21, 2008 Share Posted August 21, 2008 hey guys I want to get variables that are passed to a function. in javascript , no matter how many arguments are defined for a fuction, we can get them in the argument array. Is there a way to do such thing in php ? for example : func1 ($a,$b,$c) { . . . } Is there an array or something to keep those arguments ? like argument([0]=>$a,[1]=>$b,[2]=>$c) ? thanks Link to comment https://forums.phpfreaks.com/topic/120738-solved-getting-values-passed-to-function-like-javascript/ Share on other sites More sharing options...
stuffradio Posted August 21, 2008 Share Posted August 21, 2008 ..? You just go echo $a . $b . $c; That would output the values for a,b,c Unless I'm not following you here.. Link to comment https://forums.phpfreaks.com/topic/120738-solved-getting-values-passed-to-function-like-javascript/#findComment-622218 Share on other sites More sharing options...
BlueSkyIS Posted August 21, 2008 Share Posted August 21, 2008 you could send an array instead of separate values function doSomething($the_array) { $var0 = $the_array[0]; $var1 = $the_array[1]; $var2 = $the_array[2]; // etc. } Link to comment https://forums.phpfreaks.com/topic/120738-solved-getting-values-passed-to-function-like-javascript/#findComment-622223 Share on other sites More sharing options...
asmith Posted August 21, 2008 Author Share Posted August 21, 2008 :-\ there are like 30 variables. I have to check all, so I was hoping for an array and do a loop! apparently I can't :/ Link to comment https://forums.phpfreaks.com/topic/120738-solved-getting-values-passed-to-function-like-javascript/#findComment-622233 Share on other sites More sharing options...
BlueSkyIS Posted August 21, 2008 Share Posted August 21, 2008 yes you can. function doSomething($the_array) { $var_count = 0; foreach ($the_array AS $a_var) { ${'var'.$var_count} = $a_var; } } Link to comment https://forums.phpfreaks.com/topic/120738-solved-getting-values-passed-to-function-like-javascript/#findComment-622236 Share on other sites More sharing options...
akitchin Posted August 21, 2008 Share Posted August 21, 2008 ... all that will do is constantly reassign var0, since $var_count doesn't change. look in to func_get_args(): http://us.php.net/func_get_args Link to comment https://forums.phpfreaks.com/topic/120738-solved-getting-values-passed-to-function-like-javascript/#findComment-622246 Share on other sites More sharing options...
asmith Posted August 21, 2008 Author Share Posted August 21, 2008 @BlueSkyIS sorry to bothered you, All I wanted was not to use array inside the function. ( I had to change so many codes for changing that to an array) @akitchin Thanks a ton! Link to comment https://forums.phpfreaks.com/topic/120738-solved-getting-values-passed-to-function-like-javascript/#findComment-622289 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.