简体   繁体   English

PHP strlen问题

[英]PHP strlen question

Ok I am checking that a string is at least 4 characters long and 25 or less characters short 好的我正在检查一个字符串是否至少4个字符长,25个或更少的字符短

I tried to use strlen like this 我试着像这样使用strlen

$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? 但它不起作用......我做错了什么?

Using strlen is correct to check the length of a string (in bytes). 使用strlen是正确的,以检查字符串的长度(以字节为单位)。 But a number cannot be both smaller than 4 and greater than 25 at the same time. 但是,一个数字不能同时小于4 大于25。 Use || 使用|| instead: 代替:

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

Now the condition is fulfilled if the number is either smaller than 4 or greater than 25. 现在,如果数字小于4 大于25,则满足条件。

将&&更改为||

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

I think you want an OR there: 我想你想要一个OR:

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

Like Gumbo said, the length cannot possibly be both less than 4 AND greater than 25. && means and . 就像Gumbo所说,长度不可能都小于4且大于25. &&表示and

并摆脱'if'关键字前面的'else'!

With your code, it is evident that you are trying to validate a text field. 使用您的代码,很明显您正在尝试验证文本字段。 You have not shared html of this code. 您尚未共享此代码的HTML。 Anyways , from my expertise i will say that you should not use &&. 无论如何,根据我的专长,我会说你不应该使用&&。 You should use ||. 你应该使用||。 Also you should not directly use else if. 另外,如果你不应该直接使用else。 You should use if for this code. 您应该使用if代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM