trink Posted August 28, 2007 Share Posted August 28, 2007 In a function, how would I make a specific argument optional? Say I had something like this: function myfunction($a,$b,$c,$d){ if(!$d){ $d=1; } return $a.$b.$c.$d; } Thats basically what I'm trying to do with my function, and its not working. It returns the correct value, but I have to supress the error that it returns about not having a 4th argument with a @. So it ends up looking like @myfunction(1,2,3). I don't mind using the @ but I'd like to make the fourth argument optional. I'd appreciate any help on this topic. Quote Link to comment https://forums.phpfreaks.com/topic/66993-optional-arguments-in-functions/ Share on other sites More sharing options...
pocobueno1388 Posted August 28, 2007 Share Posted August 28, 2007 You just have to assign the optional arguments a default value, that makes it optional. <?php function myfunction($a,$b,$c,$d=1){ return $a.$b.$c.$d; } ?> So if you don't put anything for $d, it will automatically become "1" as default. If you don't want it to be anything, just put NULL in place of the 1. Quote Link to comment https://forums.phpfreaks.com/topic/66993-optional-arguments-in-functions/#findComment-335936 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.