Re: optional argument when creating a function

From: Date: Wed, 17 Aug 2005 15:13:33 +0000
Subject: Re: optional argument when creating a function
References: 1 2  Groups: php.general 
Request: Send a blank email to php-general+get-220755@lists.php.net to get a copy of this message
D A GERM wrote:
I'm throwing a warning on a function I created. I thought a & in front of the argument was supposed to make it optional. Is there something else I need to do make that argument optional?
The ampersand does not make it optional. It passes it by reference... http://us2.php.net/manual/en/language.references.php
<CODE> //I simplified the code function doEmail($username, &$link) { if (isset($link)) {
     print "$link $username";
} else {
     print "$username";
} } doEmail($arg1); doEmail($arg1, $arg2); </CODE> Here is the error: Warning: Missing argument 2 for doemail() in /srv/www/htdocs/test-a/staff/email_scramble.php on line 24
To make the second variable optional, give it a value in the argument list when defining the function... function doEmail ( $username, $link = NULL ) { -- John C. Nichel ÜberGeek KegWorks.com 716.856.9675 john@kegworks.com

« previous php.general (#220755) next »