在一个HTML文件调用PHP代码控制台显示当执行PHP代码如下结果:的意外行为如果在PHP代码语句
PHP处理:调试3:测试IF:在IF
PHP处理:调试4:假
不过,我期望的第一个控制台结果是php processing: debug 3: TEST IF: in ELSE
。例如,根据控制台,if-else语句的错误部分(if)被执行,我不明白为什么(对于这个非常简单的代码)?
有什么建议吗?
PHP代码:
//TEST CODE
if($productselected_form[0] == true)
{
$response_array['debug3'] = 'TEST IF: in IF';
}
else
{
$response_array['debug3'] = 'TEST IF: in ELSE';
}
$response_array['debug4'] = $productselected_form[0];
//send the response back
echo json_encode($response_array);
//END TEST CODE
Javascript代码(在执行console.log Ajax调用PHP代码):
console.log("php processing: debug 3: "+msg.debug3);
console.log("php processing: debug 4: "+msg.debug4);
有啥'$ productselected_form [0]'的价值,还是应该只是猜测? –
'$ productselected_form'包含什么? – Neal
做一个'var_dump($ productselected_form [0])'看看你测试的是什么。由于您使用简单的'=='相等性测试,所以PHP可以根据需要自由地进行类型转换/转换,而且很多东西会测试等于'true'。 –