Ben Phelps Posted May 18, 2007 Share Posted May 18, 2007 function newFunction($var1, $var2, $optVar1, $optVar2) { echo $var1; echo $var2; if (isset($optVar1)) { echo $optVar1; } if (isset($optVar2)) { echo $optVar2; } } I get: Warning: Missing argument 3 for newFunction() Warning: Missing argument 4 for newFunction() and i understand why, but how can I have those optional variables and not get that error. Link to comment https://forums.phpfreaks.com/topic/52063-php-function-help/ Share on other sites More sharing options...
pocobueno1388 Posted May 18, 2007 Share Posted May 18, 2007 By setting them to a default value like this: <?php function newFunction($var1=NULL, $var2=NULL, $optVar1=NULL, $optVar2=NULL) { echo $var1; echo $var2; if (isset($optVar1)) { echo $optVar1; } if (isset($optVar2)) { echo $optVar1; } } ?> Link to comment https://forums.phpfreaks.com/topic/52063-php-function-help/#findComment-256665 Share on other sites More sharing options...
per1os Posted May 18, 2007 Share Posted May 18, 2007 <?php function newFunction($var1, $var2, $optVar1=NULL, $optVar2=NULL) { echo $var1; echo $var2; if (!is_null($optVar1)) { echo $optVar1; } if (!is_null($optVar2)) { echo $optVar1; } } Link to comment https://forums.phpfreaks.com/topic/52063-php-function-help/#findComment-256672 Share on other sites More sharing options...
Ben Phelps Posted May 18, 2007 Author Share Posted May 18, 2007 Thanks for the fast answers, they both helped. Link to comment https://forums.phpfreaks.com/topic/52063-php-function-help/#findComment-256679 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.