How to simplify the process of verifying a certain value?

Asked

Viewed 112 times

0

For example, when I need to check some certain values, I create a "method", similar to that:

public function verificaString($campo)
{
    if (isset($_POST[$campo]) && (!empty($_POST[$campo]) && (is_string($_POST[$campo])))):
        (string)$var = filter_var(trim($_POST[$campo]), FILTER_SANITIZE_STRING);
        return $var;
    else:
        $var = 'Indisponível';
        return $var;
    endif;
}

Now thinking about this part more specifically:

if (isset($_POST[$campo]) && (!empty($_POST[$campo])

There is no way to simplify this process?

Every time I need to check if a variable is set and it has a value other than null, do I have to do just that? There’s no way to simplify this procedure?

3 answers

2

Utilise isset and use !empty (in your case) is redundant as empty does exactly the opposite of isset (basically he is !isset($var);) with the addition of the variable negation command. That is:

empty($var); // true

is equal to:

!isset($var) || !$var; // true

The result of empty will be the same as !isset and negation (shown above). That is, you can add in your condition that is only !empty.

Already in your method, you can use Early Returns and remove the if/else of its method which will leave it with a better reading and a less cyclomatic complexity:

public function verificaString($campo)
{
    if (empty($_POST[$campo]) || !is_string($_POST[$campo]))
    {
        return 'Indisponível';
    }

    return filter_var(trim($_POST[$campo]), FILTER_SANITIZE_STRING);
}

Notice the inversion of the instruction if that is validating whether the value, in the superglobal $_POST, is empty or different from string. If true, it returns that it is unavailable.

Else is no longer needed as Return stops code execution. If the code does not fall into the if, the string can be sanitized.

Now, knowing if the method is useful or not (simplifying the existence of the method) is something I can’t say with so little code snippets.

  • 3

    Please review your last code, it seems that the function return is_string is being passed as parameter of empty together with the value of $_POST.

  • 2

    isset is different from Empty. Empty checks for non-existence and has no value. isset checks for.

  • @Andersoncarloswoss corrected, thanks for the information

  • @Lucascarvalho read again what I wrote, Empty is the opposite of isset + the negation of the variable. See link: https://stackoverflow.com/questions/4559925/why-check-both-isset-and-empty or the manual itself

  • 1

    The isset() checks if a variable is/was set and is not null. Already the Empty() check if she is empty.

  • 2

    @lucascarvalho the empty is a language constructor and does exactly what was said in the reply, as is also mentioned in the documentation. Therefore it is possible to omit the isset if used the empty, yes.

  • @Uzumakiartanis had lacked a denial in the empty, but the written definition is still correct. See the comments above.

  • Yes, that’s right :) I’m just saying it’s not the same thing. You can do the same things, but it’s not the same things.

  • @Lucascarvalho At no time has it been said that they are the same thing. Only that they are redundant in the code.

  • 1

    It’s just like you said @Gabrielheming. Empty with isset the way it’s used is redundancy. Just use Empty that’s enough.

Show 5 more comments

2

I don’t know if this is exactly what you’re looking for but I’ll risk a cleaner and more beautiful way to check if your post variables are being passed:

Ex:

$nome = filter_input(INPUT_POST, 'nome', FILTER_SANITIZE_SPECIAL_CHARS);

Value of the requested variable in case of success, FALSE if the filter fails, or NULL if the variable_name parameter is an undefined variable. If the flag FILTER_NULL_ON_FAILURE is used, it returns FALSE if the variable is not defined and NULL if the filter fails.

For more information please follow the php manual, hugs and good studies boy!!!

2

Using the filter_input:

filter_input(INPUT_POST, $campo, FILTER_SANITIZE_STRING)

There’s a list of predefined constants for the filters.

You can then simplicate a little and reduce your function next to the operador ternário:

public function verificaString($campo)
{
    return trim(filter_input(INPUT_POST, $campo, FILTER_SANITIZE_STRING)) ?:  'Indisponível';
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.