Skip to content

Instantly share code, notes, and snippets.

@fukata
Created March 27, 2011 05:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fukata/888953 to your computer and use it in GitHub Desktop.
Save fukata/888953 to your computer and use it in GitHub Desktop.
PHP variables to be empty string or NULL in making a determination.
<?php
// bad example
$v = '0';
if ( empty($v) ) {
echo '$v is blank.';
}
// good example
$v = '0';
if ( strlen($v)==0 ) {
echo '$v is blank.';
}
// or use is_blank function
function is_blank($str) {
return strlen($v)===0;
}
if ( is_blank($v) ) {
echo '$v is blank.'
}
// It is safer to write PHP code that you can type Even with awareness.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment