PHP Function with Optional Parameters

前端 未结 14 1215
星月不相逢
星月不相逢 2020-12-02 08:05

I\'ve written a PHP function that can accepts 10 parameters, but only 2 are required. Sometimes, I want to define the eighth parameter, but I don\'t want to type in empty st

14条回答
  •  没有蜡笔的小新
    2020-12-02 08:13

    What I have done in this case is pass an array, where the key is the parameter name, and the value is the value.

    $optional = array(
      "param" => $param1,
      "param2" => $param2
    );
    
    function func($required, $requiredTwo, $optional) {
      if(isset($optional["param2"])) {
        doWork();
      }
    }
    

提交回复
热议问题