Hello! There is a code:

function($somevar_1, $somevar_2) { //code... } 

I need that if the $somevar_2 parameter is not passed, then it will be assigned a default value. The manual says that you can do this:

 function($somevar_1, $somevar_2 = 'string') { //code... } 

But the problem is that I need to set the default value as another variable. I tried to do this:

 $arg = 'string'; function($somevar_1, $somevar_2 = $arg) { //code... } 

This code does not work. Gives an error message:
Constant expression contains invalid operations

What to do?

  • set the default value, for example, null , check in the code, and if null , then replace the destination of the first argument. - teran
  • you do not think that defaults are applied at the stage of script execution. it is syntactic sugar, so to speak. if you have a($b, $c = null) and you write a(0) then this is just beautiful for you, the actual call will be a(0, null) that is, at the stage of the parser, or whatever. and so in principle in any language, I guess. - teran
  • Thank! Did as you said - set the default value to null , everything worked out - User_274

0