簡體   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