SitePoint Sponsor |
|
User Tag List
Results 1 to 7 of 7
-
Aug 12, 2007, 19:25 #1
- Join Date
- May 2007
- Location
- Toronto, ON, Canada
- Posts
- 213
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Error with multiple default arguments in a function
I have a function with default values that I need help with. Do you see anything wrong?
Here is the code:
PHP Code:function test($a, $b, $c=2, $d='hello', $e='everyone') {
echo "$d $e <br /> a = $a<br /> b=$b<br /> c=$c<br />";
}
$val = test('the letter a', 'the letter b', '', '', 'Mr. Bond');
echo $val;
Hello Mr. Bond
a = the letter a
b = the letter b
c = 2
instead I get this:
Parse error: syntax error, unexpected '=', expecting '&' or T_VARIABLE
Please let me know:
1) What is wrong with my code that gives me this error?
2) What should I put for the $c and $d arguments to get a default value instead of blanks?
Thank you!
-Frank
-
Aug 12, 2007, 20:04 #2PHP Code:
<?php
function test ($a, $b, $c = null, $d = null, $e = null) {
$c = !is_null($c) ? $c : 2;
$d = !is_null($d) ? $d : 'hello';
$e = !is_null($e) ? $e : 'everyone';
echo "$d $e <br /> a = $a<br /> b=$b<br /> c=$c<br />";
}
test('the letter a', 'the letter b', null, null, 'Mr. Bond');
-
Aug 12, 2007, 22:17 #3
- Join Date
- May 2007
- Location
- Toronto, ON, Canada
- Posts
- 213
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Thanks for the suggestion but this did not work, I get the same error message.
I am using PHP 5
-
Aug 12, 2007, 22:25 #4
It must be something else that snippet has no error
-
Aug 12, 2007, 22:32 #5
- Join Date
- Jan 2002
- Location
- Australia
- Posts
- 2,634
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
agentforte, the main problem with your initial function is that it echos directly, while your calling code expects a return value.
Change the echo to return and it'll work, but the output won't be what you were aiming for.
An empty string doesn't automatically cause an argument to be skipped...it just assigns $c and $d with empty strings. If you want to bypass them you'll need to add logic to do that inside the function (as demonstrated by logic_earth)
Another option is also to put $e as the third argument, that way you can call your function with only 3 arguments and the defaults will apply to any arguments not passed.
Make sense?
-
Aug 12, 2007, 22:46 #6
- Join Date
- May 2007
- Location
- Toronto, ON, Canada
- Posts
- 213
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
haha, sorry for being a newb. I missed a $ in front of one of the variables (The function is a lot more complex than the example I provided. I need some sleep and fresh eyes anyway...)
The logic to set default values was really helpful.
The third option Mike mentioned does not really apply, since some times $e will be provided and some times $d. So even switching would result in the same scenario (where a default is required before the last provided argument)
-
Aug 12, 2007, 22:51 #7
- Join Date
- Jan 2002
- Location
- Australia
- Posts
- 2,634
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
yes, in that case passing null and implementing logic inside the function to replace null values with the default is the way to go.
Bookmarks