Jump to content

Passing different variables to a single function?


grippat
 Share

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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...