Branden Wagner Posted November 20, 2006 Share Posted November 20, 2006 function optional arguements... ok so... i looked on php.net and found that option parameters could be declared in functions likefunction testing($var1, $var2 = 'test'){// your code}and this would make the $var2 optional.. but i cant get it to work on php5... am i doing something wrong.. or is there a different way to do it. Quote Link to comment https://forums.phpfreaks.com/topic/27816-optional-parameters/ Share on other sites More sharing options...
doni49 Posted November 20, 2006 Share Posted November 20, 2006 What you mean that it doesn't work? Some kind of error? I'm using php 5.x (not sure of the exact version) and it works for me.Try this on for size:[code]<?phpfunction test($v1, $v2 = "Blank"){ echo "<br />This is V1: " . $v1; echo "<br />This is V2: " . $v2;}test("Test1");test("Test1", "Test2");?>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/27816-optional-parameters/#findComment-127298 Share on other sites More sharing options...
JasonLewis Posted November 20, 2006 Share Posted November 20, 2006 i thought you just had to go like this:[code]function test($var1, $var2=""){//execute code.}[/code]then when you call the function you dont have to enter a value for $var2, if you dont want to. Quote Link to comment https://forums.phpfreaks.com/topic/27816-optional-parameters/#findComment-127357 Share on other sites More sharing options...
doni49 Posted November 20, 2006 Share Posted November 20, 2006 I do believe THAT's what I did. That's why I asked what made you think it didn't work. Did you get some kind of error message?The above code will display the following in the browser.[quote]This is V1: Test1This is V2: BlankThis is V1: Test1This is V2: Test2[/quote] Quote Link to comment https://forums.phpfreaks.com/topic/27816-optional-parameters/#findComment-127423 Share on other sites More sharing options...
JasonLewis Posted November 22, 2006 Share Posted November 22, 2006 yes but you dont have to place the blank in there, is all i am saying. otherwise your setting the default value to blank. it would be easier to set it as nothing, UNLESS you need a default value. depends on what the function is for i guess. Quote Link to comment https://forums.phpfreaks.com/topic/27816-optional-parameters/#findComment-128420 Share on other sites More sharing options...
wildteen88 Posted November 22, 2006 Share Posted November 22, 2006 If you want to make a variable optional you'll need to give it a default value. This is the only way to make optional parameters for functions. Quote Link to comment https://forums.phpfreaks.com/topic/27816-optional-parameters/#findComment-128769 Share on other sites More sharing options...
Barand Posted November 23, 2006 Share Posted November 23, 2006 if you want a variable number of parameters[code]<?phpfunction foo () { $n = func_num_args(); $sum = 0; for ($i=0; $i < $n; $i++) { $sum += func_get_arg($i); } return $sum;}?>[/code]echo foo (1,2,3); // --> 6echo foo (1,2,3,4,5); // --> 15 Quote Link to comment https://forums.phpfreaks.com/topic/27816-optional-parameters/#findComment-129035 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.