繁体   English   中英

使用不带参数的功能

[英]use the functions without its parameters

我在这里使用正则表达式2个功能,我想挣钱的时候,正则表达式2是这两者返回false另一个函数false ,如果没有,那么true

这里的问题是,当我想在第三个函数中使用2个regex函数时,我必须给它们提供参数,我认为这是不必要的,因为第三个函数只会返回简单的true或false。 每当在第三个函数中为两个regex函数提供参数时,我都会得到一个undefined variable

我尝试使用有效的global变量,但由于这是一种不好的做法,因此我正在寻找更好的解决方案。

码:

function regex1($input)
{
    $regex= "/^[A-Za-z0-9 ]*$/";
    if (!preg_match($regex, $input))
    {
        return false;
    }
    else
    {
        return true;
    }
}

function regex2($input)
{
    $regex= "/^[A-Za-z0-9 ]*$/";
    if (!preg_match($regex, $input)) 
    {
        return false;
    }
    else
    {
        return true;
    }
}

function checkBoth()
{
    if (regex1($input) === false || regex2($input) === false)
    {
        return false;
    }
    else
    {
        return true;
    }
}

编辑:我在我的其他文件中使用的checkBoth函数是这样,另外2个是regex函数:

 if (!regex1($input))
 {
      // show error at the same time
 }
 if (!regex2($input))
 {
      // show error at the same time
 }

if(checkBoth())
{
    // success
}
function regex2($input,$secondVar=false)
{....

稍后在需要添加代码的地方添加:

if($secondVar !== false){
// do whatever...
}

如果您不能使用“ false”,则可以将字符串''或其他任何不会出现在其中的值清空。

暂无
暂无

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

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