简体   繁体   English

PHP的strlen函数和2个变量

[英]php strlen function and 2 vars

if (strlen($title) > 2 && ($msg) > 2 && ($email) > 2)) {
 //true
} else {
 //false
}

What would be the correct way of checking more than 1 var? 检查1个以上var的正确方法是什么? This wont work..tried many ways 这是行不通的。

What you have to note is that everything between a boolean operator (eg: &&) is not related to the other. 您需要注意的是,布尔运算符(例如&&)之间的所有内容都不相互关联。

eg: strlen($a) && $b 例如: strlen($a) && $b

is not the same as: 与以下内容不同:

strlen($a) && strlen($b)

Unlike spoken language where you can say "a and b are larger then 2" you have to program it as "a is larger then 2 and b is larger then 2" 与口语不同,您可以说“ a和b大于2,而b则大于2”。

ie: 即:

if (strlen($title) > 2 && strlen($msg) > 2 && strlen($email) > 2)

If you want a reusable function that checks lengths, and takes an arbitrary number of arguments something like this would work: 如果您想要一个可重用的函数来检查长度,并接受任意数量的参数,则可以使用以下方法:

function validateLength() {

// get all passed in arguments
$args = func_get_args();
// get the first argument as the length
$len = array_shift($args);

foreach($args as $arg) {

  if (strlen($arg) < $len) return false;

}
return true;

}

Usage: 用法:

$valid = validateLength(2, $title, $msg, $email);

You just need to call strlen on the other two variables to test the length of test is > 2. Also, the last parenthesis should be removed. 您只需要在其他两个变量上调用strlen即可测试长度是否大于2。此外,最后一个括号也应删除。 Like so: 像这样:

if (strlen($title) > 2 && strlen($msg) > 2 && strlen($email) > 2) {

What about calling strlen for each one of the vars ? 为每个变量调用strlen怎么样?

if (strlen($title) > 2 && strlen($msg) > 2 && strlen($email) > 2) {
 //true
} else {
 //false
}

If you want to get the length of each variable, you should use the function that does this... on each one of those variables. 如果要获取每个变量的长度,则应在每个变量上使用执行此操作的函数。

EDIT : should test the code before posting it : there is one closing parenthese in your code that doesn't belong ;-) 编辑:应在发布代码之前对其进行测试:您的代码中有一个不包含右括号的括号;-)

If you keep it, you'll get a Parse error. 如果保留它,则会出现“解析”错误。

I think you want to do this: 我认为您想这样做:

if (strlen($title) > 2 && strlen($msg) > 2 && strlen($email) > 2) {
  //true
} else {
 //false
}

When calling a function you need to specify the function name and then the arguments to that function in parenthesis immediately following the function name. 调用函数时,您需要指定函数名称,然后在该函数名称之后立即在括号中指定该函数的参数。 If the function you want to use does not accept a variable number of arguments (like strlen for instance) you must call the function multiple times, once for each variable you have. 如果要使用的函数不接受可变数量的参数(例如, strlen ),则必须多次调用该函数,每个拥有的变量都要调用一次。

In cases like this it can often be best to encapsulate this kind of stuff in its own function like this: 在这种情况下,通常最好将此类内容封装在自己的函数中,如下所示:

function isValid($title, $msg, $email) {
    return 
        strlen($title) > 2 && 
        strlen($msg) > 2 && 
        strlen($email) > 2;
}

Then you would change your if statement to use the new function like this: 然后,您将更改if语句以使用新功能,如下所示:

if (isValid($title, $msg, $email)) {
  //true
} else {
 //false
}

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

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