PHP function with variable as default value for a parameter

No, this isn’t possible, as stated on the Function arguments manual page:

The default value must be a constant
expression, not (for example) a
variable, a class member or a function
call.

Instead you could either simply pass in null as the default and update this within your function…

function actionOne($id=null) {
    $id = isset($id) ? $id : $_GET['ID'];
    ....
}

…or (better still), simply provide $_GET[‘ID’] as the argument value when you don’t have a specific ID to pass in. (i.e.: Handle this outside the function.)

Leave a Comment