PHP函数中缺少参数-如何停止警告 [英] missing arguments in PHP Function - how to stop the warning
本文介绍了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
变量,因此我得到警告,提示Missing argument 6 for sendemail(),
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屋!
查看全文