Jump to content

php function with alot of option peramiters


thewooleymammoth

Recommended Posts

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?

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;
  }

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

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.

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.