2012-03-18 76 views
1

Ahoy all。我有一个联系表格PHP脚本。我将它用于多个站点,因为它非常快捷和简单。基本上,它通过联系表单中的所有表单字段循环,不管它们是什么。这样做,我不必一个一个手动做POST事情。关于PHP运营商的使用感到困惑

ANYWAY,我的问题很简单。下面的代码片段:现在

if ($thisField != "contact-submit") { 
    if (($thisField != "human2")) { 
     $msg .= "<b>".$thisField ."</b>: ". $thisValue ."<br>"; 
    } 
    } 

,用它做这个循环的问题是它拿起提交的所有东西,包括提交按钮和我隐藏的表单字段,以防止机器人。我不想将这些字段显示给我的客户。的

所以不要做这两个嵌套的循环,我想做得

if (($thisField != "human2") or ($thisField != "contact-submit") 

,但它只是不工作。我也曾尝试||运营商也是如此。

我错过了什么?

+0

*(相关)* [这是什么符号意味着PHP(http://stackoverflow.com/questions/3737139/reference-what-does-this-symbol-mean- in-php) – Gordon 2012-03-18 21:48:19

+0

*忘记了..... ^^ * – 2012-03-18 21:48:21

回答

2

该表达式始终评估为true。如果您将一个值与两个不同的值进行比较,则至少其中一个值是不相等的。 我想你的意思是使用and&&,所以你可以检查这个值是不是这两个值中的任何一个。

if (($thisField != "human2") && ($thisField != "contact-submit") 

if (!($thisField === "human2" or $thisField === "contact-submit")) 

if (($thisField === "human2" or $thisField === "contact-submit") === false) 
// Because you might easily overlook the exclamation mark in the second example 

或使用in_array

if (! in_array($thisField, array('human2', 'contact-submit'))) 
// Easier add extra fields. You could stick the array in a variable too, for better readability 
+0

你错了。如果'$ thisField'等于'0',那么这两个表达式都是真的。 – Gumbo 2012-03-18 21:50:06

+0

是的,所以表达整体仍然评估为真。我的确切点。 – GolezTrol 2012-03-18 21:55:17

+0

对不起,我的意思是它们是错误的,不正确:因为两个字符串都被转换为整数并且产生了'0',所以'0!='human2''为false并且'0!='contact-submit“ – Gumbo 2012-03-18 21:58:39

3

$thisField将永远不会human2或联系不上,透过(如果它是一个,它的而不是其他)。你一定意味着&&

if($thisField != "human2" && $thisField != "contact-submit") 
+0

@PaulHanak:是的,如果两个操作数都是'true','&&'返回'true'。如果它不是'human2'和'contact-submit'',这是另一种说法'不'human2'而不是'contact-submit'的方式,您希望它处理该字段。所以它更像是其中的一种“乘以两个负面因素并获得积极”的交易。 – Ryan 2012-03-18 21:52:45

+0

谢谢@minitech。我真的不得不把它分解在我脑海中,但是我去了。呵呵。有趣的是,有时候,简单的事情会让我感到困惑 – PaulHanak 2012-03-18 22:41:50