PHP IF AND statement won't execute else section


PHP IF AND statement won't execute else section

我试图在PHP中使用IF AND语句来测试

  1. 提交按钮被按下和
  2. 我的$testemail变量等于1,即PHP脚本中的有效电子邮件。

如果电子邮件是好的,我得到:谢谢你,1//(这是正确的)和电子邮件发送

如果邮件是坏的,我得到:谢谢你,0。//(这是不正确的),电子邮件仍然试图发送错误的电子邮件地址。

因为脚本正确地输出0或1,我确信它已经测试了电子邮件并正确地决定它是好是坏(因此'0''1'),但脚本从未转到else语句。我似乎不能得到IF和语句的权利。我试过"1"'1'

非常困惑。提前谢谢。

if(!filter_var($emailtmp, FILTER_VALIDATE_EMAIL))
{
  $testemail = "0";
}
else
{
  $testemail = "1";
}
global $testemail;
var_dump($testemail);
if (isset($_POST['submit']) && ($testemail = 1) )       
{       
    //Everything is good, proceed
    sendEmail(preg_replace(array('/<td>.*href="'?remove.*<'/td>/i', '/<th.*&nbsp;    
         <'/th>/i'), '', $_SESSION['SHOPPING_CART_HTML']));
    $result_message = "Thank You.  $testemail ";
}
elseif (isset($_POST['submit']))
{
    //Missing Required Information
    $result_message = "You must use a Valid email addresse, $testemail  ";
}
($testemail = 1)  
应该

($testemail === 1) 

只要($testemail = 1)就可以将testemail的值设置为1(对于if的目的,它是true,因为它成功了)。==或更好的===是你想要检查的,如果左&右值匹配

问题是您将值1分配给$testemail,而不是比较它。