validation filter_input php

Asked

Viewed 29 times

3

I’m getting paid for $_POST I would like to know whether it is wrong to validate the if thus.

$romaneio = filter_input(INPUT_POST, 'romaneio');

if($romaneio == ''){
    $romaneio = 'null';
}

2 answers

3


There is nothing wrong with your condition, what is interesting to note is the following, the PHP already considers a condition as false in the following cases:

  • null
  • 0
  • array()
  • ""

Whereas you do the following assignment:

$romaneio = filter_input(INPUT_POST, 'romaneio');

You can simply put your variable into a condition and use it normally if it is not considered false:

if($romaneio){ //simples assim, sem precisar de comparação
    //e no bloco você a utiliza como quiser
} else { //else opcional caso queira setar algo como no exemplo da pergunta
    $romaneio = 'null';
}

Or even as you put the question, but simply "denying" the condition:

if(!$romaneio){
    $romaneio = 'null'; 
}

1

The filter_input, in addition to bringing the result, also makes the field validation, bringing FALSE. I recommend doing:

if (!filter_input(INPUT_POST, 'romaneio')) {
    $romaneio = 'null';
}
  • Okay, but I need you to 'romaneio' is assigned to a variable if it is not null.

  • Can you put in the else.

Browser other questions tagged

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