[SOLVED] making a custom function argument 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
Skittlewidth
Forum Contributor
Posts: 389
Joined: Wed Nov 06, 2002 9:18 am
Location: Kent, UK

[SOLVED] making a custom function argument optional

Post by Skittlewidth »

Quick question:

When writing a custom function that can take 3 arguments, how can I make the third argument optional?
The function itself works fine with just 2 arguments but PHP displays an error if I leave the 3rd one out.

Thanks!
Last edited by Skittlewidth on Tue Apr 05, 2005 8:57 am, edited 2 times in total.
User avatar
CoderGoblin
DevNet Resident
Posts: 1425
Joined: Tue Mar 16, 2004 10:03 am
Location: Aachen, Germany

Post by CoderGoblin »

Set a default value

Code: Select all

function myfunct($var1,$var2,$var3="")
{
}
Note optional parameters must all be on the end.

You may also want to look at http://www.php.net/manual/en/function.func-num-args.php
http://www.php.net/manual/en/function.func-get-args.php
Post Reply