How can I return NULL with filter_input when the input is empty?
$title = filter_input(INPUT_POST, 'title');
var_dump($title);
it returns,
string '' (length=0)
but I want,
null
Thanks.
The NULL should have been FILTER_NULL_ON_FAILURE. But that doesn't do what you want either. Why do you need it to be NULL?
The reason you are getting string (0) is because the post field is available in the form. The field is just empty. If you want it to be NULL the field should not be there.
$foo = filter_input(INPUT_POST, 'foo', FILTER_SANITIZE_STRING, array('options' => array('default' => NULL)));
var_dump($foo);
So go with strlen(). Or make sure the field is not there when you don't need it.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With