Created
March 27, 2011 05:36
-
-
Save fukata/888953 to your computer and use it in GitHub Desktop.
PHP variables to be empty string or NULL in making a determination.
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 | |
// 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