How to simplify the process of checking a certain value?

0

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

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])

Is there a 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? Can not simplify this procedure?

    
asked by anonymous 23.08.2017 / 18:14

3 answers

2

I do not know if this is exactly what you are looking for but I will risk a cleaner and more beautiful way of checking if your variables via post are being passed:

Ex:

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

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

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

    
23.08.2017 / 18:49
2

Using filter_input :

filter_input(INPUT_POST, $campo, FILTER_SANITIZE_STRING)

There is a list of predefined constants for filters.

You can then simplify it a bit and narrow down your role with operador ternário :

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

Using isset and using !empty (in your case) is redundant, since empty does exactly the opposite of isset (basically it is !isset($var); ) with the addition of the variable deny 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 it is just !empty .

In your method, you can use early returns and remove if / else from your method, which will leave you with better reading and 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 statement if that is validating if the value in the superglobal $ _POST is empty or different from string. If true, it returns that it is unavailable.

The else is no longer necessary, as return returns execution of the code. If the code does not fall in if, the sanitization of the string can be performed.

Now, whether the method is useful or not (simplifying the method's existence) is something I can not claim with so few code snippets.

    
23.08.2017 / 18:29