Jump to content

calling a function


Recommended Posts

Is there any way i can call a function without using all its arguments? For example, consider this function:

function test($arg1,$arg2) {code....}

and then calling it like this:


I did try it but it produces an error. So, is there a way to call a function without all its arguments?

Link to comment
Share on other sites

You must pass all required arguments. If you want to specify an optional argument then you give it an initial value when you define the function: function test($arg1,$arg2 = false) If you call the function and only pass one argument, $arg2 will be false. It's not possible to specify required arguments after optional arguments: function test($arg1,$arg2 = false, $arg3) All required arguments should be listed before all optional arguments. If your function may change in the future, you might want to pass an array of arguments instead:

function test($args){  $arg1 = isset($args['arg1']) ? $args['arg1'] : '';  $arg2 = isset($args['arg2']) ? $args['arg2'] : '';  $arg3 = isset($args['arg3']) ? $args['arg3'] : '';}

Now you pass an array to the function:

test(array(  'arg1' => 'val1',  'arg2' => 2,  'arg3' => true));

That way if you change your function in the future to add new arguments then you don't need to go back and change all of the code that uses the function since it's already passing an array. You can just change the function to use that new option if it was passed in the array, or use a default value if it wasn't (or trigger an error). I use that technique with functions that have several arguments where many of them might be optional, e.g.:

function handle_file_upload($options){  $input = !empty($options['input']) ? $options['input'] : trigger_error('handle_file_upload: No input name specified', E_USER_ERROR);  $dest = !empty($options['dest_dir']) ? $options['dest_dir'] : trigger_error('handle_file_upload: No destination directory specified', E_USER_ERROR);  $dest_fname = !empty($options['dest_fname']) ? $options['dest_fname'] : '';  $overwrite = !empty($options['overwrite']) ? $options['overwrite'] : false;  $mode = !empty($options['mode']) ? $options['mode'] : false;  $allowed_ext = isset($options['allowed_ext']) && is_array($options['allowed_ext']) ? $options['allowed_ext'] : false;    if (!is_dir($dest))	trigger_error('handle_file_upload: Destination directory does not exist', E_USER_ERROR);  if (!isset($_FILES[$input]))	trigger_error('handle_file_upload: The input file was not found', E_USER_ERROR); ...

So some of those options are required and will trigger an error if they are missing, and others are optional and will use default values if they weren't given.

Link to comment
Share on other sites

This code scheme with the array seems a little complicated to me, using... arg2=false(for example)in the argument list, seems more convenient to me. Thanks for the clarification.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Create New...