validation filter_input php

2

I'm receiving by $_POST I'd like to know if it's wrong to validate if like this.

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

if($romaneio == ''){
    $romaneio = 'null';
}
    
asked by anonymous 23.11.2016 / 17:59

2 answers

2

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

  • null
  • 0
  • array ()
  • "

Whereas you make the following assignment:

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

You can simply put your variable in 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'; 
}
    
23.11.2016 / 18:53
0

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

if (!filter_input(INPUT_POST, 'romaneio')) {
    $romaneio = 'null';
}
    
23.11.2016 / 18:02