PHP的“echo”无法正常工作
我需要检查一些函数的返回值,我把它们放在一个while循环中:PHP的“echo”无法正常工作,php,if-statement,while-loop,echo,Php,If Statement,While Loop,Echo,我需要检查一些函数的返回值,我把它们放在一个while循环中: //should be true if an error comes up (a function returns false) $error = false; while ($error == false) { if (!$this->check_date()) { $this->log('bad date'); $error =
//should be true if an error comes up (a function returns false)
$error = false;
while ($error == false) {
if (!$this->check_date()) {
$this->log('bad date');
$error = true;
break;
}
if (!$this->check_location()) {
$this->log('bad location');
$error = true;
break;
}
if (!$this->check_abc()) {
$this->log('bad abc');
$error = true;
break;
}
//... more if's
break;
}
//No error - great
if ($error == false) {
//Answer to my AJAX call
echo "true";
} else {
$this->log('-There is an error-');
}
那么,有什么问题
我没有得到AJAX调用的输出
但是如果我做一个回声测试;在这里:
//... more if's
break;
}
echo "test";
//No error - great
if ($error == false) {
//Answer to my AJAX call
echo "true";
} else {
$this->log('-There is an error-');
}
我得到以下回复:
那么,这是怎么回事
AJAX代码:
$.ajax({
url: "index.php",
type: "POST",
async: false,
data: "do=this",
success: function (answer) {
console.log(answer);
},
error: function (jXHR, textStatus, errorThrown) {
console.log("error" + errorThrown);
}
});
感谢您的帮助:
解决方案PHP似乎误解了事实-因此我们需要对其进行enode,例如作为JSON字符串:
//No error - great
if ($error == false) {
//Answer to my AJAX call
echo json_encode("true");
} else {
$this->log('-There is an error-');
}
您响应AJAX请求,因此应该使用以确保返回有效的json
<?php
echo json_encode('true');
在你的例子中,它只是一个字符串,但PHP正在进行一种类型转换,因此使用json_编码更安全。我没有得到我的AJAX调用的输出-也许你没有发布的AJAX内容有问题。在while循环中使用它有什么意义?/@FunkFortyNiner仙人掌的好日子不?@Xorifelse它肯定是buddy:-@Xorifelse如果发生错误就中断,这样就不会执行以下函数。@MickaelLeger循环的底部有一个中断。。。