比较一个字符串和一个只有数字的字符串
我是新来的PHP,并一直试图没有成功得到这个比较工作:
我有一个可变的'productCode'可能
我需要知道变量productCode中的内容是什么? 它仍然是'选择'或'102'(就像只有数字的字符串)
我应该使用哪种比较运算符?
它具有“选择”的价值吗? 它应该是
我试过了
if(strcmp($productCode,'Choose')){
}//tried double quotes also
if(($productCode=='Choose')){
}//tried double quotes also
if(($productCode==='Choose')){
}//tried double quotes also
我无意中发现了这一点..'102'与'选择'相比也在通过if语句...
有人可以帮助我一个正确的方法来检查这种混合案件..
编辑1:相关代码(在合并了Rizier123的=== 0建议之后:
 $productCode11=$_POST['productCode11'];
 $productCode12=$_POST['productCode12'];
 $productCode13=$_POST['productCode13'];
if(strcmp($productCode11,'Choose') === 0){
    //testing the values if they actually hold what I am expecting
    echo '<script type="text/javascript">alert(" '.$productCode11.' ");</script>';
    echo '<script type="text/javascript">alert(" '.$productCode12.' ");</script>';
        //testing if the 'if' check passes
    echo '<script type="text/javascript">alert("same");</script>';
        $productCode11Error="Fill This";
        $incomplete=true;
    }
if(strcmp($productCode12,'Choose') === 0)){
        $productCode12Error="Fill This";
        $incomplete=true;
    }
if(strcmp($productCode13,'Choose') === 0){
        $productCode13Error="Fill This";
        $incomplete=true;
    }
编辑2我做了一些测试,这些都是我得到的奇怪结果:
$test = strcmp($productCode11, 'Choose');
$test2 = strcmp($productCode11, $productCode12);
if both are 'Choose' I get 1 and it is incremented by 1 for every submission (2,3,4,5...)
if both are numbers and same, I get 0
if LHS is number and RHS is 'Choose', I get -1
if LHS and RHS are 'Choose' I get 1,3,4..
if LHS is 'Choose' and RHS is number I get 1
if both are numbers, and LHS > RHS I get 1
if both are numbers and RHS > LHS I get -3
  你的第一次尝试就像它应该那样工作。  但是你必须知道, strcmp()返回0,当字符串相等时,所以只需添加一个比较,例如 
if(strcmp($productCode, 'Choose') === 0) {
                                //^^^^^ See here
}
  因此,如果$productCode等于"Choose" ,它将评估为TRUE。 
边注:
strcmp()区分大小写,表示: aA != aa strcasecmp()不区分大小写,表示aA == aa   经过漫长的试验和错误之后,我怀疑从$ POST返回的变量可能有一些额外的字符。  为了解决这个问题,我尝试使用trim()函数。  令人惊讶的是,一切都开始工作'预期'。 
(而且我坚持Rizier123的=== 0建议)
链接地址: http://www.djcxy.com/p/58587.html