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. Quote 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; } } ?> Quote 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; } } Quote 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. Quote Link to comment https://forums.phpfreaks.com/topic/52063-php-function-help/#findComment-256679 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.