用功能改变php代码

时间:2014-03-10 13:03:07

标签: php

我遇到一段代码问题。这段代码工作正常。没问题。

include ("CCheckMail.php"); 
$checkmail = new CCheckMail ();
$emails = array ("korkula@iservicesmail.com"); 
foreach ($emails as $email) 
{ 
    if ($checkmail->execute ($email)) { return 1; } else { return 0; } 
}// end foreach

现在,我想更改为使用数组,我想发送电子邮件以检查var,如下所示:

include ("CCheckMail.php");
funtion_name($email){
    $checkmail = new CCheckMail ();
    if ($checkmail->execute ($email)) { return 1; } else { return 0; } 
}


} //end function

新代码不起作用我不明白为什么。

任何人都可以帮助我吗?

谢谢!

1 个答案:

答案 0 :(得分:0)

尝试声明对象一次,然后通过将其传递给函数以及您正在检查的电子邮件地址来重复使用它。

//Call the checkMail class
include("CCheckMail.php");

//Define the object to work with
$checkmail = new CCheckMail();

//Function to send the emails and return response
function checkEmailFunc($email, $emailObject){    
    if ($emailObject->execute ($email)) { return 1; } else { return 0; } 
}

//Test it out
$emails = array("korkula@iservicesmail.com", "test@example.com");
foreach ($emails as $email) { 
    if (checkEmailFunc($email, $checkmail)) {
        echo "Success! Checked " . $email . "<br/>";
    } else {
        echo "Failed to check " . $email . "<br/>";
    }
}