Jump to content

Passing different variables to a single function?


grippat

Recommended Posts

I have a function that I call twice. I have to send it different variables the two times I call it though. One time it's just an ID number, another time it's a different type of ID number and a string. When this function gets called it says It's missing either 1 or two variables depending upon which call. I thought there was a way to set the variables in the function definition so that if a certain variable wasn't passed to it, it would just set it to a default value. Is there a way to do this?

Link to comment
Share on other sites

function doSomething ( $required_argument, $optional_argument = "some data"){   //code }

Just give a default value to the optional argument.When calling the function you can pass both arguments or only first one.Make sure to list the required arguments first, this would be incorrect

function doSomething ( $optional_argument = "some data", $required_argument){   //code }

Link to comment
Share on other sites

Archived

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

×
×
  • Create New...