PHP的响应不如预期

时间:2014-02-26 04:09:24

标签: php json

我正在尝试根据从应用程序发送到服务器的内容将数据从数据库发送到应用程序,如下所示:

$search_bp=$_POST['search_bp'];
$search_trig = 0;

    if(strcmp($search_bp,"1")==true){
    //decisions that could set search_trig = 1;
    //eg
    if(strcmp($_POST['EStype'],'Event')){
       if(strcmp($_POST['type'],'Any')==false){
           if(strcmp($_POST['type'],$row["type"])==false){
           $search_trig=1;//doesnt match specs
          }
      }
    }   
    }
if($search_trig == 0){
    $event["pid"] = $row["pid"];
    $event["name"] = $row["name"];
    $event["longitude"] = $row["longitude"];
    $event["latitude"] = $row["latitude"];
    $event["pavement"] = $row["pavement"];
    $event["traffic"] = $row["traffic"];
    $event["environment"] = $row["environment"];
    $event["image_b64"] = $row["image_b64"];
    $event["date"] = $row["date"];
    $event["time"] = $row["time"];
    $event["type"] = $row["type"];
    // push single product into final response array
    array_push($response["events"], $event);
}
    $response["success"] = 1;
    // echoing JSON response
    echo json_encode($response);

出现问题的是strcmp($search_bp,"1")似乎总是假的,即使我发送它

params.add(new BasicNameValuePair("search_bp",  Integer.toString(search_bp)));

我知道search_bp=1,我只是对php知之甚少,所以我很确定这只是我的语法。

提前谢谢你,

泰勒

2 个答案:

答案 0 :(得分:2)

strcmp返回一个不是布尔值的整数。 Returns < 0 if str1 is less than str2; > 0 if str1 is greater than str2, and 0 if they are equal.

从技术上讲,您应该只是选中if ($search_bp == "1") {

如果我们要在php的松散型世界中剖析你的陈述,你可以看到你的错误。

`strcmp` will return 0 because they do match. 
0 = false 
1 = true

所以if(strcmp($search_bp,"1")==true){解决了if(0==true){,然后if(0==1){解决了不符合条件的问题。

答案 1 :(得分:1)

请注意 strcmp 不返回true或false:

返回如下

0 - if the two strings are equal
<0 - if string1 is less than string2
>0 - if string1 is greater than string2

您需要更改以下内容:

if(strcmp($search_bp,"1")==0){
//
}

在您的情况下,您也可以使用==运算符。

请注意: == 仅返回 true false ,它不会告诉您哪个是“更大”的字符串