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

时间:2017-08-31 03:36:22

标签: php if-statement strlen

我已经实施了用户注册表单,我需要检查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.';}
          }

        }
      }
    }
  }

1 个答案:

答案 0 :(得分:1)

如你所说,你的strlength应该是11那么你的if语句应该是这样的

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