Created
September 20, 2016 10:50
-
-
Save bernbecht/df29997b04d1fb294bbc6f5c649fe3d0 to your computer and use it in GitHub Desktop.
Checking if a field is really empty
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function isFieldEmpty($field) { | |
return isset($field) && trim($field) == ''; | |
} | |
$data = '0'; | |
echo isFieldEmpty($data); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.