arik arik - 4 months ago 5x
PHP Question

PHP pass default argument to function

I have a PHP function, like this:

function($foo = 12345, $bar = false){}

What I want to do, is call this function with the default argument of $foo passed, but $bar set to true, like this (more or less)

function(DEFAULT_VALUE, true);

How do I do it? How do I pass an argument as a function's default value without knowing that value?

Thanks in advance!


This is not natively possible in PHP. There are workarounds like using arrays to pass all parameters instead of a row of arguments, but they have massive downsides.

The best manual workaround that I can think of is defining a constant with an arbitrary value that can't collide with a real value. For example, for a parameter that can never be -1:

define("DEFAULT_ARGUMENT", -1);

and test for that:

function($foo = DEFAULT_ARGUMENT, $bar = false){}