2

Ok I am checking that a string is at least 4 characters long and 25 or less characters short

I tried to use strlen like this

$userNameSignupLength = strlen($userNameSignup);

else if($userNameSignupLength<4 && $userNameSignupLength>25) {

            $userNameSignupError = "Must be between 4 to 25 characters long";

        }

but it doesn't work... what did I do wrong?

MrEnder
  • 265
  • 3
  • 7
  • 16
  • Same as @Kevin. And what you want is `>= 25` not `> 25`. – p4bl0 Mar 22 '10 at 14:28
  • You might want to look at http://www.php.net/manual/en/function.mb-strlen.php , strlen counts multibyte characters like UTF-8 incorrectly. –  Mar 22 '10 at 14:29
  • @p4blo, no - 25 is ok, so he wants to check `> 25` for ones which are too long. – nickf Mar 22 '10 at 14:29

5 Answers5

8

Using strlen is correct to check the length of a string (in bytes). But a number cannot be both smaller than 4 and greater than 25 at the same time. Use || instead:

if ($userNameSignupLength < 4 || $userNameSignupLength > 25)

Now the condition is fulfilled if the number is either smaller than 4 or greater than 25.

Gumbo
  • 643,351
  • 109
  • 780
  • 844
  • thank you very much lol I do that all the time mess up my ands and ors >< one day I will get it lol ^.^ – MrEnder Mar 22 '10 at 14:30
2

Change the && to ||

else if ($userNameSignupLength<4 || $userNameSignupLength>25)
Kevin
  • 13,153
  • 11
  • 60
  • 87
2

I think you want an OR there:

else if($userNameSignupLength < 4 || $userNameSignupLength > 25) {

Like Gumbo said, the length cannot possibly be both less than 4 AND greater than 25. && means and.

Andy Shellam
  • 15,403
  • 1
  • 27
  • 41
2

and get rid of the 'else' in front of the 'if' keyword!

Chris Walsh
  • 3,423
  • 2
  • 42
  • 62
-1

With your code, it is evident that you are trying to validate a text field. You have not shared html of this code. Anyways , from my expertise i will say that you should not use &&. You should use ||. Also you should not directly use else if. You should use if for this code.

atif
  • 87
  • 1
  • 3