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?

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.