asmith Posted June 25, 2009 Share Posted June 25, 2009 Hi, How do you pass optional parameters for a function. for example : I want to pass 2 arguments and the 3rd is optional : myfunction ($a, $b[, $c]); // if $c is set, send it I typed it like php explanation of functions. But apparently it gives me a parsing error. Quote Link to comment https://forums.phpfreaks.com/topic/163643-arguments-in-a-function-shothand/ Share on other sites More sharing options...
Richard Posted June 25, 2009 Share Posted June 25, 2009 To make the third (final) argument optional you could give it a default value, for example; <?php myfunction($a, $b, $c = '') { } Quote Link to comment https://forums.phpfreaks.com/topic/163643-arguments-in-a-function-shothand/#findComment-863429 Share on other sites More sharing options...
asmith Posted June 25, 2009 Author Share Posted June 25, 2009 Yea sure. I meant not sending at all. nvm, I wanted to know if there's any syntax for it. Quote Link to comment https://forums.phpfreaks.com/topic/163643-arguments-in-a-function-shothand/#findComment-863440 Share on other sites More sharing options...
Richard Posted June 25, 2009 Share Posted June 25, 2009 Have a look at; func_num_args() func_get_arg() func_get_args() Quote Link to comment https://forums.phpfreaks.com/topic/163643-arguments-in-a-function-shothand/#findComment-863444 Share on other sites More sharing options...
asmith Posted June 25, 2009 Author Share Posted June 25, 2009 Thanks for the effort mate. I meant SENDING parameters to a function(Using it), Not the syntax in the function structure. Quote Link to comment https://forums.phpfreaks.com/topic/163643-arguments-in-a-function-shothand/#findComment-863453 Share on other sites More sharing options...
Mark Baker Posted June 25, 2009 Share Posted June 25, 2009 myfunction($a, $b, $c = '') { return $a.' '.$b.' '.$c.'<br />'; } echo myfunction(1,2,3); echo myfunction(1,2); Quote Link to comment https://forums.phpfreaks.com/topic/163643-arguments-in-a-function-shothand/#findComment-863474 Share on other sites More sharing options...
asmith Posted June 25, 2009 Author Share Posted June 25, 2009 lol I'm very very very sorry guys for this stupid question. How do you write it with the if statement? for example : echo myfunction(1,2, (if 2 ==2? 5 : don't send anything)); In other words, the shorthand of this : if (2 == 2) echo myfunction(1,2,5); else echo myfunction(1,2); Quote Link to comment https://forums.phpfreaks.com/topic/163643-arguments-in-a-function-shothand/#findComment-863521 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.