0

I am making a PHP script.

My script has a function that looks something like this:

function myFunction($param1 = "foo", $param2 = "bar") {}

Let's say I want to specify a value for $param2, but want to skip $param1 as the default value is fine, how could I do something like this:

myFunction(param2)?

Tl;Dr: Can I skip certain parameters when calling a function?

3
  • 1
    No. Unfortunately, php doesn't allow this. If you want to specify a parameter on the right, you always have to specify the parameters on the left. Commented May 29, 2018 at 22:42
  • Oh man, that sucks. Oh well - thanks anyway! Commented May 29, 2018 at 22:48
  • kind of a way around this is to use an array as the argument, then you can 'skip' array keys with out issue function myFunction($param1 = array()) {} myFunction(array('a'=>7)) Commented May 29, 2018 at 23:05

1 Answer 1

2

When you using optional parameter you will have to put it at the end. There's no way to "skip" an argument other than to specify a default like false or null.

Sign up to request clarification or add additional context in comments.

1 Comment

Ok, that's unfortunate :( Thanks though!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.