简体   繁体   中英

use the functions without its parameters

I am using 2 regex functions here and I wanna make another function which returns false when the 2 regex are both false and if not, then true .

The problem here is when I wanna use the 2 regex functions in the third one, I have to give them parameters, which is not necessary I think, because the third function will only return a simple true or false. I get an undefined variable whenever I give parameters to the 2 regex functions in the 3rd one.

I tried using global variables which works but since its a bad practice I am looking for a better solution.

Code:

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;
    }
}

EDIT: The checkBoth function I am using in my other file like this together with the other 2 regex functions:

 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)
{....

Later in code in place where you need just add:

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

If you can't user "false" you can just empty string '' or any other value that will not appear there.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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