Jump to content

designing a PHP function


monkeytooth

Recommended Posts

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

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))

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.