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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.