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

时间:2013-09-05 10:14:28

标签: php function arguments

我有一个PHP函数来发送电子邮件,

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);
        }
    }

当我调用该函数时,有时候没有$cc变量,所以我收到警告说Missing argument 6 for sendemail(),

如果$ cc无效,最好的方法是停止警告吗?

4 个答案:

答案 0 :(得分:7)

如果您编写了该功能,则可以将第6个参数设为可选:

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;
    }
}

然后您可以选择省略此参数:

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");

答案 1 :(得分:1)

使用此

function sendemail($email_to,$email_from,$email_subject,$email_body,$email_replyto,$cc = "")

答案 2 :(得分:0)

试试这个,

function sendemail($email_to,$email_from,$email_subject,$email_body,$email_replyto,$cc=NULL)

把$ cc = NULL。 因此,如果没有$ cc,您将不会收到警告。

答案 3 :(得分:0)

如果您能够更改发送电子邮件功能:

function sendemail ($email_to, $email_from, $email_subject, $email_body, $email_replyto, $cc=null) { }

只需确保功能正文本身不会出现null $cc的问题。