Make a function's parameter optional?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
JAB Creations
DevNet Resident
Posts: 2341
Joined: Thu Jan 13, 2005 6:44 pm
Location: Sarasota Florida
Contact:

Make a function's parameter optional?

Post 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>';
User avatar
greyhoundcode
Forum Regular
Posts: 613
Joined: Mon Feb 11, 2008 4:22 am

Re: Make a function's parameter optional?

Post 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.
User avatar
JAB Creations
DevNet Resident
Posts: 2341
Joined: Thu Jan 13, 2005 6:44 pm
Location: Sarasota Florida
Contact:

Re: Make a function's parameter optional?

Post 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 = "";}
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: Make a function's parameter optional?

Post 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);
}

;)
There are 10 types of people in this world, those who understand binary and those who don't
User avatar
JAB Creations
DevNet Resident
Posts: 2341
Joined: Thu Jan 13, 2005 6:44 pm
Location: Sarasota Florida
Contact:

Re: Make a function's parameter optional?

Post 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:
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: Make a function's parameter optional?

Post 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!
There are 10 types of people in this world, those who understand binary and those who don't
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Re: Make a function's parameter optional?

Post 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.
User avatar
JAB Creations
DevNet Resident
Posts: 2341
Joined: Thu Jan 13, 2005 6:44 pm
Location: Sarasota Florida
Contact:

Re: Make a function's parameter optional?

Post 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");
Post Reply