PHP 函数中缺少参数 - 如何停止警告 [英] missing arguments in PHP Function - how to stop the warning

查看:29
本文介绍了PHP 函数中缺少参数 - 如何停止警告的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个发送电子邮件的 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);
        }
    }

当我调用函数时,有时没有 $cc 变量,所以我收到一个警告说 缺少 sendemail() 的参数 6,

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

如果 $cc 无效,停止警告的最佳方法是什么?

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

推荐答案

如果你写了那个函数,你可以将第 6 个参数设为可选:

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

这篇关于PHP 函数中缺少参数 - 如何停止警告的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆