mwstewart Posted April 1, 2009 Share Posted April 1, 2009 Hi all, I want to instantiate a class but only pass $var3 if it is actually set. $result = $class->_method($var1, $var2, $var3); What is the most efficient way to do this? Is there a way to do this check within the method call brackets? i.e. something like: $result = $class->_method($var1, $var2 (if(!empty($var3){"," . $var3})); I obviously need to include the comma before $var3 if it isn't empty. Thanks in advance! Link to comment https://forums.phpfreaks.com/topic/152065-if-statement-within-function-call/ Share on other sites More sharing options...
trq Posted April 1, 2009 Share Posted April 1, 2009 Take a look at call_user_func Link to comment https://forums.phpfreaks.com/topic/152065-if-statement-within-function-call/#findComment-798540 Share on other sites More sharing options...
manny Posted April 1, 2009 Share Posted April 1, 2009 Hi all, I want to instantiate a class but only pass $var3 if it is actually set. $result = $class->_method($var1, $var2, $var3); What is the most efficient way to do this? Is there a way to do this check within the method call brackets? i.e. something like: $result = $class->_method($var1, $var2 (if(!empty($var3){"," . $var3})); I obviously need to include the comma before $var3 if it isn't empty. Thanks in advance! the best way to do this is separating the function call from the condition ///$result = $class->_method($var1, $var2 (if(!empty($var3){"," . $var3})); $var3 = ( !empty($var3) ? $var3 : 0 ); ////this will set $var3 to 0 or basically nothing $result = $class->_method($var1, $var2,$var3); inside the function you can set a check like this if($var3) { //action goes here } Hope this helps Link to comment https://forums.phpfreaks.com/topic/152065-if-statement-within-function-call/#findComment-798553 Share on other sites More sharing options...
mwstewart Posted April 1, 2009 Author Share Posted April 1, 2009 Thanks guys, that's great. Link to comment https://forums.phpfreaks.com/topic/152065-if-statement-within-function-call/#findComment-798590 Share on other sites More sharing options...
manny Posted April 1, 2009 Share Posted April 1, 2009 Any time Link to comment https://forums.phpfreaks.com/topic/152065-if-statement-within-function-call/#findComment-798615 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.