/    Sign up×
Community /Pin to ProfileBookmark

Function Arguments

My function starts as follows:

[code=php]<?php
function my_function($argument1, $argument2, &$argument3 = NULL, &$argument4 = NULL, &$argument5 = NULL){
?>[/code]

As you can see arguments 3, 4 and 5 are optional. With this construct is it possible to specify arguments 1, 2 and 5 only without specifying arguments 3 and 4 when the function is called or do I have to specify all arguments to be able to allocate a value to argument 5?

to post a comment
PHP

5 Comments(s)

Copy linkTweet thisAlerts:
@NogDogJul 26.2005 — Normally you'd have to have something in the intervening places, even if it's just "". However, only a variables can be passed by reference, so the assignment of default values to a referenced variable (&$arg3 = NULL) is invalid. Therefore, if you want to pass a variable by reference, it essentially becomes a required parameter (or at least will throw a warning if you don't include it).
Copy linkTweet thisAlerts:
@bokehauthorJul 26.2005 — the assignment of default values to a referenced variable (&$arg3 = NULL) is invalid. [/QUOTE] I don't really understand this. What is invalid? Also can you tell me correct use of the '&' sign here.

I tried not using '= NULL' but it throws a warning when the argument hasn't been included in the call to the function.

Here's an example:

[code=php]null_function();


function null_function($arg1, $arg2 = NULL){
if(isset($arg1)){
print '$arg1 is set<br>';
}else{
print '$arg1 is not set<br>';
}
if(isset($arg2)){
print '$arg2 is set<br>';
}else{
print '$arg2 is not set<br>';
}
}[/code]


Here's the warning:

Warning: Missing argument 1 for null_function()

notice, no warning on $arg2 and the variable can still be accessed from the function if it is included in the call.

So what would be valid, and, what is the correct use of '&' ?
Copy linkTweet thisAlerts:
@NogDogJul 26.2005 — I don't really understand this. What is invalid? Also can you tell me correct use of the '&' sign here.[/quote]
'&' is used for variable references. [b]$foo = &$bar[/b] refers $foo to $bar. Whatever value $bar has, $foo will have. If you change the value of either one, the other will now have that value.

When you pass a variable by reference to a function by using the '&' in front of one of the function's parameter variables. this means that (1) you will call the function with a variable name for that parameter and (2) that function variable and the variable you called it with will now be referenced together as described above: change the value of the function variable within the function, and the value of the variable you called it with will then have the same value. It's functionally similar to using a global variable within a function, but you can have it link to a different variable each time you call the function.

As such, it only has meaning if you call the function with a variable name for that parameter - you cannot call it with a literal string or numeric value, which will throw a fatal error. Thus it is likewise meaningless to try and set a default value - this throws a (confusing) parse error on my PHP4 installation. Ultimately, this means that any call by reference in a function parameter list must be a required argument.

I tried not using '= NULL' but it throws a warning when the argument hasn't been included in the call to the function.

Here's an example:

[code=php]null_function();


function null_function($arg1, $arg2 = NULL){
if(isset($arg1)){
print '$arg1 is set<br>';
}else{
print '$arg1 is not set<br>';
}
if(isset($arg2)){
print '$arg2 is set<br>';
}else{
print '$arg2 is not set<br>';
}
}[/code]


Here's the warning:

Warning: Missing argument 1 for null_function()

notice, no warning on $arg2 and the variable can still be accessed from the function if it is included in the call.

So what would be valid, and, what is the correct use of '&' ?[/QUOTE]

Yep, any argument which does not have a "= 'value'" is a required one.

Basically, avoid the calling by reference thing unless you have a good reason to use it. (Personally, I've not had any reason to use it for any actual project.) For a lot more info: http://us2.php.net/manual/en/language.references.php
Copy linkTweet thisAlerts:
@bokehauthorJul 27.2005 — So basicly from what I understand, the following:

[code=php]$string = 'NogDog';

function bold(&$string){
$string = "<b>$string</b>";
}[/code]


would modify the external string without any need to do a return($string) from the function.

The reason I was confused was because I saw this:
[code=php]function raqgetmxrr($sHostName, &$aMXHosts, &$aWeights = NULL)[/code]
here: [URL=http://php.net/getmxrr]http://php.net/getmxrr[/URL]
Copy linkTweet thisAlerts:
@NogDogJul 27.2005 — This:
[code=php]
function bold(&$string){
$string = "<b>$string</b>";
}
bold($text);
[/code]

...would be functionally equivalent to this:
[code=php]
function bold($string){
return("<b>$string</b>");
}
$text = bold($text);
[/code]

It becomes more powerful when there are more than one variable you want to change within the function.

As far as that example, it seems incorrect to me, and my experiments would indicate it would not work; but then again references are not something I've worked with much, so I might be misunderstanding something here.
×

Success!

Help @bokeh spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.17,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...