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? Link to comment https://forums.phpfreaks.com/topic/186915-designing-a-php-function/ Share on other sites More sharing options...
Mchl Posted January 2, 2010 Share Posted January 2, 2010 function someFunction($var1, $var2 = null) { //function body } Link to comment https://forums.phpfreaks.com/topic/186915-designing-a-php-function/#findComment-987054 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)) Link to comment https://forums.phpfreaks.com/topic/186915-designing-a-php-function/#findComment-987055 Share on other sites More sharing options...
monkeytooth Posted January 2, 2010 Author Share Posted January 2, 2010 excellent, thank you. Link to comment https://forums.phpfreaks.com/topic/186915-designing-a-php-function/#findComment-987057 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 Link to comment https://forums.phpfreaks.com/topic/186915-designing-a-php-function/#findComment-987058 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. Link to comment https://forums.phpfreaks.com/topic/186915-designing-a-php-function/#findComment-987160 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.