Jump to content

if statement within function call


mwstewart

Recommended Posts

 

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

 

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.