PHP Optional Parameters - specify parameter value by name?

后端 未结 4 1344

I know it is possible to use optional arguments as follows:

function doSomething($do, $something = \"something\") {

}

doSomething(\"do\");
doSomething(\"do         


        
4条回答
  •  春和景丽
    2020-12-02 00:44

    PHP has no named parameters. You'll have to decide on one workaround.

    Most commonly an array parameter is used. But another clever method is using URL parameters, if you only need literal values:

     function with_options($any) {
          parse_str($any);    // or extract() for array params
     }
    
     with_options("param=123&and=and&or=or");
    

    Combine this approach with default parameters as it suits your particular use case.

提交回复
热议问题