For Programmers: Free Programming Magazines  


Home > Archive > PHP Programming > June 2005 > skipping arguments in function call









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author skipping arguments in function call
Chuck Anderson

2005-06-09, 3:56 am

I have a function with 7 inputs. The last three have default values.

I want to call that function specifying the first four, skip two and
then specify the last.

I thought I could write this as :

$retval = myfunction(arg1, arg2, arg3, arg4,,,arg7);

.... but Php does not seem to want to let me do this. I get a parse
error until I supply values for the 5th and 6th arguments.

My question is simply this; is skipping arguments like that not allowed
in Php?

--
*****************************
Chuck Anderson • Boulder, CO
http://www.CycleTourist.com
Integrity is obvious.
The lack of it is common.
*****************************
Aidan

2005-06-09, 3:56 am

My suggestion would be to reorganise the order in which the function takes
it arguments. Simply change it so that the 7th argument is the 5th, and the
5th and 6th argumentss become the 6th and 7th.

That's what I'd do at least. HTH


"Chuck Anderson" <websiteaddress@seemy.sig> wrote in message
news:houdnQf0jJq5CzrfRVn-hw@comcast.com...
>I have a function with 7 inputs. The last three have default values.
>
> I want to call that function specifying the first four, skip two and then
> specify the last.
>
> I thought I could write this as :
>
> $retval = myfunction(arg1, arg2, arg3, arg4,,,arg7);
>
> ... but Php does not seem to want to let me do this. I get a parse error
> until I supply values for the 5th and 6th arguments.
>
> My question is simply this; is skipping arguments like that not allowed in
> Php?
>
> --
> *****************************
> Chuck Anderson . Boulder, CO
> http://www.CycleTourist.com
> Integrity is obvious.
> The lack of it is common.
> *****************************



Chuck Anderson

2005-06-09, 3:56 am

Aidan wrote:

>My suggestion would be to reorganise the order in which the function takes
>it arguments. Simply change it so that the 7th argument is the 5th, and the
>5th and 6th argumentss become the 6th and 7th.
>
>That's what I'd do at least. HTH
>
>

Well, yes, that would work. I can even supply the 5th and 6th arguments
in this case, but I was wondering if that really is something that Php
does not allow (the ,,, - method of skipping arguments) or if I need to
look closer for another parse error.

>
>"Chuck Anderson" <websiteaddress@seemy.sig> wrote in message
>news:houdnQf0jJq5CzrfRVn-hw@comcast.com...
>
>
>
>
>
>



--
*****************************
Chuck Anderson • Boulder, CO
http://www.CycleTourist.com
Integrity is obvious.
The lack of it is common.
*****************************
Aidan

2005-06-09, 3:56 am


"Chuck Anderson" <websiteaddress@seemy.sig> wrote in message
news:Cs2dneTX7cMrNDrfRVn-1w@comcast.com...
> Aidan wrote:
>
> Well, yes, that would work. I can even supply the 5th and 6th arguments in
> this case, but I was wondering if that really is something that Php does
> not allow (the ,,, - method of skipping arguments) or if I need to look
> closer for another parse error.


Well, the method you're using is essentially (to the best of my knowledge)
passing null values to the arguments you're trying to skip. Here's a page
from php.net I think you should read:

http://au3.php.net/manual/en/functi...riable-arg-list

HTH


>
>
>
> --
> *****************************
> Chuck Anderson • Boulder, CO
> http://www.CycleTourist.com
> Integrity is obvious.
> The lack of it is common.
> *****************************



Aidan

2005-06-09, 3:56 am


"Aidan" <nospam.aweraw@gmail.com> wrote in message
news:newscache$nnushi$rt6$1@titan.linknet.com.au...
>
> "Chuck Anderson" <websiteaddress@seemy.sig> wrote in message
> news:Cs2dneTX7cMrNDrfRVn-1w@comcast.com...
>
> Well, the method you're using is essentially (to the best of my knowledge)
> passing null values to the arguments you're trying to skip. Here's a page
> from php.net I think you should read:
>
> http://au3.php.net/manual/en/functi...riable-arg-list
>
> HTH


Ahh, try this link: http://php.net/manual/en/functions.arguments.php

>
>
>
>



Jamie Meyers

2005-06-09, 3:56 am


"Chuck Anderson" <websiteaddress@seemy.sig> wrote in message
news:houdnQf0jJq5CzrfRVn-hw@comcast.com...
>I have a function with 7 inputs. The last three have default values.
>
> I want to call that function specifying the first four, skip two and then
> specify the last.
>
> I thought I could write this as :
>
> $retval = myfunction(arg1, arg2, arg3, arg4,,,arg7);


if you wrote myfunction, then declare it like this

function myfunction(arg1, arg2, arg3, arg4, arg5="", arg6="", arg7)...
Where "" should be set to the default values.

>
> ... but Php does not seem to want to let me do this. I get a parse error
> until I supply values for the 5th and 6th arguments.
>
> My question is simply this; is skipping arguments like that not allowed in
> Php?
>
> --
> *****************************
> Chuck Anderson . Boulder, CO
> http://www.CycleTourist.com
> Integrity is obvious.
> The lack of it is common.
> *****************************



Chuck Anderson

2005-06-09, 8:56 pm

Aidan wrote:

>"Aidan" <nospam.aweraw@gmail.com> wrote in message
>news:newscache$nnushi$rt6$1@titan.linknet.com.au...
>
>
>
>Ahh, try this link: http://php.net/manual/en/functions.arguments.php
>
>
>

I have read those pages about functions and function arguments from the
manual, but I can't find any indication about whether arguments can be
skipped in the call to the function, as I cited above. It discusses
setting a default value of NULL in the function definition, but I don't
see it addressing skipping arguments in the function call. It says to
put the arguments with default values on the right side, and then it
sort of implies that you may not be able to leave out arguments.

I'm not sure where I learned the ",,," syntax for skipping arguments
(C?), but I'm beginning to think that Php simply does not support it.

I could use Variable-length argument lists, but I didn't want to go there.
[color=darkred]

--
*****************************
Chuck Anderson • Boulder, CO
http://www.CycleTourist.com
Integrity is obvious.
The lack of it is common.
*****************************
Chuck Anderson

2005-06-09, 8:56 pm

Jamie Meyers wrote:

>"Chuck Anderson" <websiteaddress@seemy.sig> wrote in message
>news:houdnQf0jJq5CzrfRVn-hw@comcast.com...
>
>
>
>if you wrote myfunction, then declare it like this
>
>

I did write it.

>function myfunction(arg1, arg2, arg3, arg4, arg5="", arg6="", arg7)...
>Where "" should be set to the default values.
>
>

That's actually something the manual says not to do. It says all default
arguments should be on the right and then has an example showing why it
won't work the other way.

My arg 7 has a default value, though, so you're structure is how it
already exists.
[color=darkred]
>
>

--
*****************************
Chuck Anderson • Boulder, CO
http://www.CycleTourist.com
Integrity is obvious.
The lack of it is common.
*****************************
Andy Hassall

2005-06-09, 8:56 pm

On Wed, 08 Jun 2005 19:06:52 -0600, Chuck Anderson <websiteaddress@seemy.sig>
wrote:

>I have a function with 7 inputs. The last three have default values.
>
>I want to call that function specifying the first four, skip two and
>then specify the last.
>
>I thought I could write this as :
>
>$retval = myfunction(arg1, arg2, arg3, arg4,,,arg7);
>
>... but Php does not seem to want to let me do this. I get a parse
>error until I supply values for the 5th and 6th arguments.
>
>My question is simply this; is skipping arguments like that not allowed
>in Php?


It's not allowed.

One alternative if you want to skip any argument and you can't rearrange the
skippable ones all towards the end is to pass an associative array instead,
which make it looks like named parameters from other languages, e.g.:

myfunction(array(
arg2 => 'wibble',
arg6 => 'wobble'
));

--
Andy Hassall / <andy@andyh.co.uk> / <http://www.andyh.co.uk>
<http://www.andyhsoftware.co.uk/space> Space: disk usage analysis tool
Chuck Anderson

2005-06-10, 3:57 am

Andy Hassall wrote:

>On Wed, 08 Jun 2005 19:06:52 -0600, Chuck Anderson <websiteaddress@seemy.sig>
>wrote:
>
>
>
>
> It's not allowed.
>
> One alternative if you want to skip any argument and you can't rearrange the
>skippable ones all towards the end is to pass an associative array instead,
>which make it looks like named parameters from other languages, e.g.:
>
>myfunction(array(
> arg2 => 'wibble',
> arg6 => 'wobble'
> ));
>
>
>

Thanks. Yes, ... I can make "other arrangements." I just wanted to be
sure that was "illegal" before I wrote off ever trying it again.

--
*****************************
Chuck Anderson • Boulder, CO
http://www.CycleTourist.com
Integrity is obvious.
The lack of it is common.
*****************************
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com