元名称不区分大小写

时间:2013-04-03 13:30:00

标签: php wordpress

我有以下GET功能,它基于常规的WordPress自定义字段名称。 勾选后,它会将自定义字段值设置为1的所有帖子排序。 它目前有效。但是,我碰巧有两个自定义字段:'free'和'twofree'

当我勾选'免费'时,它还包括'twofree',反之亦然。它似乎不区分大小写。这有什么工作吗?

<?php 
/* my code starts from  here*/
 if( isset( $_GET['show'] ) && !empty ( $_GET['show']  )  ){
   if( $_GET['show'] == 1 ) 
    $meta_field = 'free';
  else  if( $_GET['show'] == 4 )
    $meta_field = 'sale';
   else if( $_GET['show'] == 2 )
    $meta_field = 'genuine';
    else if ( $_GET['show'] == 'onfire' )
    $meta_field = 'onfire';
    else if( $_GET['show'] == 5 )
    $meta_field = 'twofree';    
 else if( $_GET['show'] == 3 )
    $meta_field = 'onfire'; 

        if( $_GET['show'] == 'sale' )
            query_posts('cat='.$cat.'&meta_key='.$meta_field.'&meta_value > 0');
        else
          query_posts('cat='.$cat.'&meta_key='.$meta_field.'&meta_value>=1');   
 }
/* my code ends from  here*/ 
?>
编辑:我发现了问题并且在部分中撒谎

query_posts('cat='.$cat.'&meta_key='.$meta_field.'&meta_value>=1');

我把它改成了

query_posts('cat='.$cat.'&meta_key='.$meta_field.'&meta_value=1');

1 个答案:

答案 0 :(得分:2)

如果要匹配精确值,请使用标识运算符===,而不是基于字符串/整数检查类似值的==


更多这个主题,请查看此link或以下答案

Using the `==` operator (*Equality*)

    true == 1; //true, because 'true' is converted to 1 and then compared
    "2" == 2 //true, because 2 is converted to "2" and then compared

Using the `===` operator (*Identity*)

    true === 1 //false
    "2" === 2 // false

This is because the **equality operator == does type coercion**...meaning that the interpreter implicitly tries to convert the values and then does the comparing.

On the other hand, the **identity operator === does not do type coercion**, and so thus it does not convert the values of the values when comparing