Page 1 of 1

Make a function's parameter optional?

Posted: Thu Sep 04, 2008 8:04 am
by JAB Creations
How can I make the $uploads parameter optional? That is if it's not defined that it won't cause an error?

Code: Select all

function file_path($id, $uploads)
{
 $member_id = get_id_length($_SESSION['member_id']);
 $path1 = str_split($member_id, 4);
 return $uploads.$path1[0].'/'.$path1[1].'/'.$filename;
}
echo '<p>file path = '.file_path($_SESSION['member']).'</p>';

Re: Make a function's parameter optional?

Posted: Thu Sep 04, 2008 8:13 am
by greyhoundcode
You can specify default values for each argument:

Code: Select all

function square($x = 0)
{
    return ($x * $x);
}
If you now call the function without specifying anything, ie square(), it will simply return 0.

Re: Make a function's parameter optional?

Posted: Thu Sep 04, 2008 8:17 am
by JAB Creations
Thanks for your reply but that would require me to throw in an if statement to determine if $uploads equaled "0" as it's going to create an invalid path. I'm hoping there is some way of doing this without assigned it a value other than $uploads = "" if I have to define it.

This didn't work...

Code: Select all

if (!isset($uploads)) {$uploads = "";}

Re: Make a function's parameter optional?

Posted: Thu Sep 04, 2008 9:32 am
by VladSun
Jab, that only shows that your "show-me-an-example" learning approach is not good ... Or even - it's not good for you.

As it is an induction-style learning approach you should have made the induction:

Code: Select all

function str_square($x = "")
{
    return ($x * $x);
}

;)

Re: Make a function's parameter optional?

Posted: Thu Sep 04, 2008 9:42 am
by JAB Creations
I tried something very similar to that and I had a parse error, then trying that I realized that I wouldn't be able to actually receive the value from the variable already defined before (and outside of) the function.

Yeah my learning "style" is sort of like the clearance rack at a store that has like fifty mark downs. :| I think I've made some progress...I read php.net pretty much daily now. :mrgreen:

Re: Make a function's parameter optional?

Posted: Thu Sep 04, 2008 9:43 am
by VladSun
JAB Creations wrote:Yeah my learning "style" is sort of like the clearance rack at a store that has like fifty mark downs. :| I think I've made some progress...I read php.net pretty much daily now. :mrgreen:
This is the better approach ;)
Congrats!

Re: Make a function's parameter optional?

Posted: Thu Sep 04, 2008 10:23 am
by onion2k
JAB Creations wrote:Thanks for your reply but that would require me to throw in an if statement to determine if $uploads equaled "0" as it's going to create an invalid path. I'm hoping there is some way of doing this without assigned it a value other than $uploads = "" if I have to define it.
The implication of this is that you're not checking the value of $uploads in the function. You should be checking to make sure it contains a value that won't break the rest of the function code regardless of where the value came from.

Re: Make a function's parameter optional?

Posted: Thu Sep 04, 2008 11:48 am
by JAB Creations
NULL! Kind of like my head after the first couple dozen attempts. :mrgreen:

Code: Select all

function test($neccesary, $optional = NULL) {return '<p>'.$neccesary.$optional.'</p>';}
echo test("neccesary");
echo test("neccesary", "optional");