我正在尝试将一串数字发送到服务器并拆分每个数字。如果数据库中存在该数字,则分配一个“ t”值,如果不存在,则分配一个“ f”值。但是由于某种原因,我只能得到t。
<?php
# get phone from uid
include_once 'utils.php';
$phone = $_GET['phone'] or die("isRegistered: missing phone");
$response = "";
$phonearray = explode(":", $phone);
for ($i = 0; $i<sizeof($phonearray); $i++){
$result = findRow("phone", $phonearray[i], "user_device") or "true" ;
if($result == "true")
{
$response = $response."t".":";
$result = "";
}
else
{
$response = $response."f".":";
$result = "";
}
}
die($response);
?>
实际上这里有几个问题。
如其他答案所述,您错误地't'
在代码的两个分支中使用了。
您似乎正在使用字符串"true"
而不是boolean true
。虽然它可能由于PHP在类型之间转换值的方式而起作用,但您实际上可能打算使用true
,而使用字符串代替可能导致以后出现意外行为。
我不知道该怎么findRow()
做,但是通过在其末尾添加or true
(甚至or "true"
)$result
将始终为true
。
您i
用来引用$phonearray
而不是$i
。i
将生成PHP警告,并将其解释为"i"
-而不是$i
变量的值。
如果您看一下代码的这一部分:
$result = findRow("phone", $phonearray[i], "user_device") or "true" ;
if($result == "true")
{
$response = $response."t".":";
$result = "";
}
else
{
$response = $response."t".":";
$result = "";
}
通过这样重写它,您将获得更好的结果:
$result = findRow("phone", $phonearray[$i], "user_device");
$response .= ($result ? 't' : 'f') . ':';
我猜测是什么原因findRow()
,因为您未包含任何内容-但我假设它只返回true / false值。
您会注意到,我还if/else
通过使用三元运算符将整个语句简化为几行。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句