开发者

Is there a PHP function that does this?

I just wrote this function for a project I am working on:

function valuesValid()
{
    $argCount = func_num_args();

    for ($i = 0; $i < $argCount; $i++)
    {
        $currentArg = func_get_arg($i);

        if (!isset($currentArg) || empty($currentArg))
            return false;
    }

    return true;
}

It seems like something that might have been impleme开发者_JS百科nted before so I was wondering if that was the case.


It seems like your function does exactly the same of the && operator to me

$value1 = valuesValid($a, $b, $c);

$value2 = $a && $b && $c;

var_dump($value1 === $value2);


This is not a necessary validation, as many input field are meant to be left empty on many forms.

There is no general form of Validation, so there is no general function for validation. You should implement your own logic for that.

Keep in mind that only checking for non-empty values is not a satisfying validation.


If understand it correctly, it checks if the values passed to the function are set correctly, and are not empty.

Well, I think that the simple func_get_args() would get all of your values and a foreach() would be simpler.


You should name your function values_filled() rather, if that is its purpose. The isset() check within is redundant, only the empty() test makes sense. Usually you want to use strlen to check for the presence of form fields however.

if (values_filled($_POST["field"], $_POST["form"], $_POST["text"])) {

As said in the comments you will already get a notice at this point for missing fields. But then the whole if could just be written as:

if ($_POST["field"] && $_POST["form"] && $_POST["text"]) {
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜