【问题标题】:'IF' statements in PHPPHP中的'IF'语句
【发布时间】:2012-02-07 10:34:01
【问题描述】:

我不确定我的 IF 语句是否正确 -

  • 我希望代码结束如果变量$answer 不是'4''four'

  • 如果变量 $answer 等于 4 或 4 - 程序继续处理代码。

感谢您的帮助。

谢谢

if( ! in_array($answer, array(4, 'four')))
{   
       //if answer is not in an array containing either 4 or four then..
        echo "You have entered the security question incorrectly. Your request will NOT be processed";
        exit();
}
else
{
    if ($from != "")
    {
            //test send mail to Melanie 
            mail($to, $subject, $contents, $from_header);

            // redirect back to url visitor came from
            $display_blockmsg = "Thank you, <b>$_POST[title] $_POST[firstname] $_POST[lastname]</b>, <br>
                                Your contact form details have been sent to us <br>
                                Your contact details are:<br><br>
                                <b>Address:</b> $_POST[address1] 
                                $_POST[address2],<br>
                                <b>City:</b> $_POST[city]<br>
                                <b>County:</b> $_POST[county]<br>
                                <b>Postcode:</b> $_POST[postcode]<br>
                                <b>Country:</b> $_POST[country]<br>
                                <b>Telephone:</b> $_POST[telephone]<br>
                                <b>email:</b> $_POST[from]<br><br>";        
  }
  else      
  {     
      $display_blockmsg = "<center><b>The email field</b> is empty. Please go back and complete this field.</center>"; 
  }
} //end of first else ...if

【问题讨论】:

    标签: php conditional-operator


    【解决方案1】:

    试试这个

    if( ! in_array($answer, array('4', 'four'))) 而不是if( ! in_array($answer, array(4, 'four')))

    $answer 是一个字符串,在数组中它是一个数字(整数)

    所以更好用

    if( $answer != 4 &amp;&amp; $answer != 'four')

    【讨论】:

      【解决方案2】:

      if 控件看起来像

      if(// condition is true) { 
        $that
      } elseif (// another condition is true ) { 
        // anotherthing 
      } else { 
        // somethingelse 
      }
      

      如果退出 if 语句,其余代码将不会被执行。

      手册很有帮助 - http://php.net/manual/en/control-structures.if.php

      【讨论】:

        【解决方案3】:

        对于两个选项,您不需要创建数组并使用数组选项。就用这个吧:

        if ($answer != 4 && $answer != 'four')
        

        【讨论】:

        • 嗨,谢谢到目前为止,我的括号在喷口上?unfort。我不能重新粘贴代码?请帮助
        猜你喜欢
        • 2015-07-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-03-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多