monkeytooth Posted January 2, 2010 Share Posted January 2, 2010 This may be a simple question, I don't know. I'm tempting to make a function. Scratch that I have made a function. But I have recently appended more to it. Problem is with what I have appended to it, the possible call to the function goes from something like: someFunction($var1); to someFunction($var1,$var2); In concept not an issue. However not every call to the function will require $var2. So how do I make $var2 act as an optional $var? Quote Link to comment Share on other sites More sharing options...
Mchl Posted January 2, 2010 Share Posted January 2, 2010 function someFunction($var1, $var2 = null) { //function body } Quote Link to comment Share on other sites More sharing options...
oni-kun Posted January 2, 2010 Share Posted January 2, 2010 As the above post says, It makes $var2 null by default, meaning it is only used if it is defined (you can check if it is and ignore it with isset) function someFunction($var1, $var2 = null) { return $var1; if (isset($var2)) { return $var2; } } echo someFunction('Hello'); //valid echo someFunction('Hello', 'World'); //valid Although you can use somefunction(array($arr)) Quote Link to comment Share on other sites More sharing options...
monkeytooth Posted January 2, 2010 Author Share Posted January 2, 2010 excellent, thank you. Quote Link to comment Share on other sites More sharing options...
Mchl Posted January 2, 2010 Share Posted January 2, 2010 function someFunction($var1, $var2 = null) { return $var1; if (isset($var2)) { return $var2; } } oni-kun: Now find out why this example doesn't make any sense Quote Link to comment Share on other sites More sharing options...
monkeytooth Posted January 2, 2010 Author Share Posted January 2, 2010 you know, i didn't read into this much last night as I ended up working on something else til I passed out, but looking at it now, I get understand how that could not work. Quote Link to comment 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.