thewooleymammoth Posted October 14, 2010 Share Posted October 14, 2010 I created a function with a lot of optional peramiters. I dont necissarily want to use every option peramiter. As i recall you can do something like this <?php function($param, $param2, {$optional='value', $optional2='value2'}); //or function($param, $param2, {$optional='value', $optional2='value2'}); ?> but both break my code. And i cant seem to find the correct syntax anywhere... Am i mistaken in thinking you can do this? Quote Link to comment https://forums.phpfreaks.com/topic/215894-php-function-with-alot-of-option-peramiters/ Share on other sites More sharing options...
BlueSkyIS Posted October 14, 2010 Share Posted October 14, 2010 function some_function($param1, $param2, $optional1='value', $optional2='value') { // function code } Quote Link to comment https://forums.phpfreaks.com/topic/215894-php-function-with-alot-of-option-peramiters/#findComment-1122300 Share on other sites More sharing options...
BlueSkyIS Posted October 14, 2010 Share Posted October 14, 2010 alternatively, you can provide no parameters and get the number of parameters and each parameter within the function using func_num_args() and func_get_arg(): function concatenate() { $result = ""; for ($i = 0;$i < func_num_args();$i++) { $result .= func_get_arg($i) . " "; } return $result; } Quote Link to comment https://forums.phpfreaks.com/topic/215894-php-function-with-alot-of-option-peramiters/#findComment-1122301 Share on other sites More sharing options...
thewooleymammoth Posted October 14, 2010 Author Share Posted October 14, 2010 i think you misunderstood. I know how to use optional peramiters. I want to know the syntax for calling the function. I dont want to have to input all the optional parameters to input the last one, $optional5. So as i remember, you can use brackets inside the function call and then use the variable name to assign variables out of order.... <?php function useOptionalParams($value1, $value2, $optional='', $optional2='',$optional3='',$optional4='',$optional5=''){ echo $value1.$value2.$optional.$optional2; } useOptionalParams($param, $param2, {$optional='value', $optional5='value2'});//breaks code??? idk why //or useOptionalParams($param, $param2 {$optional='value', $optional5='value2'});//breaks code??? idk why ?> Quote Link to comment https://forums.phpfreaks.com/topic/215894-php-function-with-alot-of-option-peramiters/#findComment-1122305 Share on other sites More sharing options...
BlueSkyIS Posted October 14, 2010 Share Posted October 14, 2010 yes, i misunderstood. I don't know if it's possible to do what you're trying. I guess if I was going to write a function, I wouldn't give it so many optional parameters, but instead pass an array of parameters. Quote Link to comment https://forums.phpfreaks.com/topic/215894-php-function-with-alot-of-option-peramiters/#findComment-1122307 Share on other sites More sharing options...
BlueSkyIS Posted October 14, 2010 Share Posted October 14, 2010 here is an apparent option: http://www.seoegghead.com/software/php-parameter-skipping-and-named-parameters.seo Quote Link to comment https://forums.phpfreaks.com/topic/215894-php-function-with-alot-of-option-peramiters/#findComment-1122309 Share on other sites More sharing options...
PFMaBiSmAd Posted October 14, 2010 Share Posted October 14, 2010 You use empty parameters - function('val1','val2',,,'val5'); Quote Link to comment https://forums.phpfreaks.com/topic/215894-php-function-with-alot-of-option-peramiters/#findComment-1122310 Share on other sites More sharing options...
thewooleymammoth Posted October 14, 2010 Author Share Posted October 14, 2010 damn, i was thinking of javascript. http://www.openjs.com/articles/optional_function_arguments.php I guess thats what i get for taking a break from web design... thanks for trying! The reason i didnt want to make it an array instead of optional paramaters is that that the value is already an array of array of arrays, (complicated function) And i didnt want to add another layer of arrays, but i guess im gonna have too. Quote Link to comment https://forums.phpfreaks.com/topic/215894-php-function-with-alot-of-option-peramiters/#findComment-1122318 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.