【问题标题】:PHP if statement using !empty() not properly checking existing variablesPHP if 语句使用 !empty() 未正确检查现有变量
【发布时间】:2017-05-18 17:49:33
【问题描述】:

我遇到了一个奇怪的问题,我似乎无法正确调试。 我目前正在使用 AngularJS 和对 PHP 文件的 JSON 请求创建一个注册表单。这是我用于调试的输入数据示例。

现在发送的请求清楚地包含以下数据,如调试 JSON 响应所见。

现在由于某种原因,$response['debug-two']$response['debug-info-two'] 不会添加到响应的 JSON 中,即使所需的所有字段都是 !empty()true。此外,$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);

【问题讨论】:

  • 这一行有错字; $reponse['empty-fields'] = true; - 应该是$response['empty-fields'] = true;(你错过了's')。你到底想用 if 语句检查什么?
  • @thebluefox 哦,是的,这让空白字段消失了,但为什么会这样呢?当所有字段都是 !empty() 并且协议 1 为真时?
  • 做一点调试,看看哪些表达式是错误的。 var_dump((!empty($fullname)) . ' && ' .(!empty($email)) . ' && ' .(!empty($birthday)) . ' && ' .(!empty($password1)) . ' && ' .(!empty($password2)) . ' && ' .($agreement1) );
  • @thebluefox 找到问题了,谢谢! :)

标签: php angularjs json if-statement


【解决方案1】:

您有几个错字导致您出现问题。

第一个在$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);

【讨论】:

    猜你喜欢
    • 2021-09-18
    • 1970-01-01
    • 2013-06-20
    • 2017-08-12
    • 2019-08-28
    • 2012-09-17
    • 1970-01-01
    • 2017-04-06
    • 2015-10-14
    相关资源
    最近更新 更多