defining optional variables within a function

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
staypufinpc
Forum Newbie
Posts: 12
Joined: Thu May 01, 2003 7:59 pm

defining optional variables within a function

Post by staypufinpc »

I have created a number of successful functions with required arguments, but every time I try to create a variable with optional arguments it doesn't work. I get a parse error about missing a '(' somewhere but that never seems to be the case. They're simple functions, really. I just have some functions that output html forms. I'd like to pass optional arguments to show the form(s) filled out and whatnot. How do you correctly define optional arguments?
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

Code: Select all

<?php
function func($param, $optional1=0, $optional2='example')
{
	echo $param, ' ', $optional1, ' ', $optional2;
}

func('a', 'simple');
?>
see also: http://www.php.net/manual/en/functions. ... ts.default
http://www.php.net/manual/en/function.func-get-args.php
staypufinpc
Forum Newbie
Posts: 12
Joined: Thu May 01, 2003 7:59 pm

IT worked

Post by staypufinpc »

Thanks, I was trying: function func($param [, $optional1]]). Ooops.
Post Reply