繁体   English   中英

来自PHP的响应未如预期

[英]Response from PHP not as expected

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

$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不太了解,所以我很确定这只是我的语法。

先感谢您,

泰勒

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){不会满足该条件。

请注意, 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){
//
}

您也可以使用==运算符。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM