使用 strlen() 函数检查字符串是否有 11 个字符

Checking whether string has 11 characters using strlen() function

我已经实现了一个用户注册表,我需要检查 ABN 字段是否恰好有 11 个字符。我为此写了一个 if else 语句。但据此,即使有人输入 11 个字符,它也会弹出错误消息。可能 if-else 语句有问题。虽然我是编程新手,但很难找到错误。我也更改了 if-else 位置,但我无法修复它。任何形式的帮助将不胜感激。我会把我的编码放在下面。谢谢!

if($_POST){
  //validate email
  if(empty($_POST['email']) || empty($_POST['password']) || empty($_POST['confirm']) || empty($_POST['firstname']) || empty($_POST['lastname']) ||empty($_POST['companyname']) || empty($_POST['companyphone'])||empty($_POST['abn'])||empty($_POST['type'])||empty($_POST['position'])||empty($_POST['phone']) || empty($_POST['address1']) || empty($_POST['city']) || empty($_POST['state']) || empty($_POST['country']) || empty($_POST['postcode'] )){
    $errors[] = 'You must fill out required field.';
  } else {
    if (!filter_var($email, FILTER_VALIDATE_EMAIL)){
      $errors[] = 'You must enter a valid email.';
    }else {
      $query = "SELECT * FROM users WHERE email=?";
      $stmt = $db->prepare($query);
      $stmt->bind_param("s", $email);
      $stmt->execute();
      $result = $stmt->get_result();
      $user = mysqli_fetch_assoc($result);
      $stmt->execute();
      $stmt->store_result();
      $userCount = $stmt->num_rows();
      // Check if the email already exists in database.
      if($userCount > 0){
        $errors[] = 'That email already exists.';
      } else {
        //check if password is not less than 6 characters
        if (strlen($password) < 6) {
          $errors[] = 'Password must be at least 6 characters.';
        }else {
          // check if passwords match
          if($password !=$confirm){
            $errors[] = 'The passwords do not match.';
          }
          else{
            if(strlen($abn)< 11)
            {$errors[] = 'ABN Should be 11 characters.';}
          }

        }
      }
    }
  }

正如您所说,您的 strlength 应该正好是 11 所以您的 if 语句应该是这样的

if(strlen($abn)!=11) { $errors[] ='ABN Should be 11 characters.'; }