dongzipu7517 2013-09-05 02:14
浏览 72
已采纳

PHP函数中缺少参数 - 如何停止警告

I have a PHP function to send emails,

function sendemail($email_to,$email_from,$email_subject,$email_body,$email_replyto,$cc)
    {
        if(filter_var($email_to, FILTER_VALIDATE_EMAIL))
        {
            require_once "/usr/local/lib/php/Mail.php";

            $from = $email_from;
            $to = $email_to;
            $subject = $email_subject;
            $body = $email_body;

            $host = "mail.domain.co.uk";
            $username = "sending@domain.co.uk";
            $password = "********";

            $headers = array ('From' => $from,
              'To' => $to,
              'Cc' => $cc,
              'Subject' => $subject,
              'Content-type' => 'text/html');
            $smtp = Mail::factory('smtp',
              array ('host' => $host,
             'auth' => true,
             'username' => $username,
             'password' => $password));

             $rec = $to.', '.$cc;

            $mail = $smtp->send($rec, $headers, $body, $cc);
        }
    }

when i call the function, sometimes there is no $cc variable so i get a warning saying that Missing argument 6 for sendemail(),

whats the best way to stop the warning if $cc is not valid?

展开全部

  • 写回答

4条回答 默认 最新

  • duanji4449 2013-09-05 02:17
    关注

    If you wrote that function, you can make the 6th parameter optional:

    function sendemail($email_to, $email_from, $email_subject, $email_body, $email_replyto, $cc = null) {
        if ($cc !== null) {
            // add cc headers, e.g.
            // $headers['Cc'] = $cc;
        }
    }
    

    You will then have the option to omit this parameter:

    sendemail("to@example.com", "from@example.com", "subject", "body", "replyto@example.com");
    sendemail("to@example.com", "from@example.com", "subject", "body", "replyto@example.com", "cc@example.com");
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部