使用PHP if语句!empty()未正确检查现有变量
我遇到了一个奇怪的问题,似乎无法正确调试。 我目前正在使用AngularJS和对PHP文件的JSON请求创建一个注册表单。下面是一个用于调试的输入数据示例 现在,通过调试JSON响应,发送的请求清楚地包含以下数据 由于某种原因,使用PHP if语句!empty()未正确检查现有变量,php,angularjs,json,if-statement,Php,Angularjs,Json,If Statement,我遇到了一个奇怪的问题,似乎无法正确调试。 我目前正在使用AngularJS和对PHP文件的JSON请求创建一个注册表单。下面是一个用于调试的输入数据示例 现在,通过调试JSON响应,发送的请求清楚地包含以下数据 由于某种原因,$response['debug-two']或$response['debug-info-two']不会添加到响应的JSON中,即使所有必需的字段都是!空()。而且,$reponse['empty-fields']也不是。因此,它必须归结为if语句中的空字段,但我似
$response['debug-two']
或$response['debug-info-two']
不会添加到响应的JSON中,即使所有必需的字段都是!空()。而且,$reponse['empty-fields']
也不是。因此,它必须归结为if语句中的空字段,但我似乎无法理解或弄清楚原因,因为所有字段都设置为$response['debug-info']
# Prevent XSRF
if ($session->checkXSRF()) {
# Get POST data
$data = json_decode(file_get_contents("php://input"));
$fullname = $data->fullname;
$email = $data->email;
$birthday = $data->bithday;
$password1 = $data->password1;
$password2 = $data->password2;
$agreement1 = $data->agreement1;
$agreement2 = $data->agreement2;
$response['debug'] = $data;
$response['debug-info'] = $fullname.$email.$birthday.$password1.$password2.$agreement1.$agreement2;
# Check if empty fields
if ( (!empty($fullname)) && (!empty($email)) && (!empty($birthday)) && (!empty($password1)) && (!empty($password2)) && ($agreement1) ) {
$response['debug-two'] = $data;
$response['debug-info-two'] = $fullname.$email.$birthday.$password1.$password2.$agreement1.$agreement2;
} else {
# All fields are required
$reponse['empty-fields'] = true;
}
} else {
# XSRF Error detected
$response['xsrf-invalid'] = true;
}
# Return JSON response
echo json_encode($response);
你有几处打字错误导致了你的问题
第一个是在$birthday=$data->bithday中代码>-您错过了“生日”中的“r”
其次,在$reponse['empty-fields']=true中代码>您错过了“响应”中的“s”
# Prevent XSRF
if ($session->checkXSRF()) {
# Get POST data
$data = json_decode(file_get_contents("php://input"));
$fullname = $data->fullname;
$email = $data->email;
$birthday = $data->birthday;
$password1 = $data->password1;
$password2 = $data->password2;
$agreement1 = $data->agreement1;
$agreement2 = $data->agreement2;
$response['debug'] = $data;
$response['debug-info'] = $fullname.$email.$birthday.$password1.$password2.$agreement1.$agreement2;
# Check if empty fields
if ( (!empty($fullname)) && (!empty($email)) && (!empty($birthday)) && (!empty($password1)) && (!empty($password2)) && ($agreement1) ) {
$response['debug-two'] = $data;
$response['debug-info-two'] = $fullname.$email.$birthday.$password1.$password2.$agreement1.$agreement2;
} else {
# All fields are required
$response['empty-fields'] = true;
}
} else {
# XSRF Error detected
$response['xsrf-invalid'] = true;
}
# Return JSON response
echo json_encode($response);
这行有一个打字错误<代码>$reponse['empty-fields']=true代码>-应为$response['empty-fields']=true代码>(您错过了“s”)。你到底想用if语句检查什么?@thebluefox哦,是的,这让空字段消失了,但为什么它会这样做呢?当所有的字段都被删除时!empty()和agreement1为真?进行一些调试,看看哪些表达式为假<代码>变量转储((!empty($fullname))。&&(!empty($email))。&&(!empty($birth))。&&(!empty($password1))。&(!empty($password2))。&&($agreement1))代码>@thebluefox发现了问题,谢谢!:)