Skip to content

Instantly share code, notes, and snippets.

@bernbecht
Created September 20, 2016 10:50
Show Gist options
  • Save bernbecht/df29997b04d1fb294bbc6f5c649fe3d0 to your computer and use it in GitHub Desktop.
Save bernbecht/df29997b04d1fb294bbc6f5c649fe3d0 to your computer and use it in GitHub Desktop.
Checking if a field is really empty
<?php
function isFieldEmpty($field) {
return isset($field) && trim($field) == '';
}
$data = '0';
echo isFieldEmpty($data);
?>
@bernbecht
Copy link
Author

bernbecht commented Sep 20, 2016

A common mistake, especially among newbies, is to replace the isset()
and trim() combination with a call to PHP’s empty() function,
which tells you if a variable is empty. This isn’t usually a good idea,
because empty() has a fatal flaw: it’ll return true even if a variable contains
the number 0.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment