Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP strlen question

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?

like image 307
MrEnder Avatar asked Dec 03 '25 10:12

MrEnder


2 Answers

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.

like image 199
Gumbo Avatar answered Dec 05 '25 00:12

Gumbo


Change the && to ||

else if ($userNameSignupLength<4 || $userNameSignupLength>25)
like image 31
Kevin Avatar answered Dec 05 '25 01:12

Kevin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!