dongle2627 2017-07-05 09:30
浏览 75
已采纳

如何在php中只提供第二个可选参数?

I have this function:

function generic_notify($subject = "notify triggered", $message = "<p>Generic notification</p>", $to = "pavel.janicek@mydomain.mydomain"){
    EDD()->emails->send( $to, $subject, $message );
}

In some cases, I am completely fine to just call generic_notify(); and know that some function was called, because the above will send me mail.

In other cases, I would like to just change message, or just subject or send it to other email.

If I wanted to just change the message, is it possible? And if yes, how?

  • 写回答

1条回答 默认 最新

  • dt20081409 2017-07-05 09:57
    关注

    You can't but if I were you just put a condition inside that function

    function generic_notify($subject = null, $message = null, $to = null){
    
        // your default value
        $defaultSubject = "notify triggered";
        $defaultMessage = "<p>Generic notification</p>";
        $defaultTo = "pavel.janicek@mydomain.mydomain";
    
        $to = (!empty($to)) ? $to : $defaultTo;
        $subject = (!empty($subject)) ? $subject : $defaultSubject;
        $message = (!empty($message)) ? $message : $defaultMessage;
    
            echo "Subject: ".$subject ."<br>";
            echo "Message: ".$message ."<br>";
            echo "TO: ".$to."<br>";
    }
    
    // if you want not to change all the default parameter just leave blank
    generic_notify();
    
    // if you want to change the default parameter just put a value that is not null on it
    generic_notify(null, '<p>Change Message</p>', null);
    

    Demo

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!