Advertisement
jchaven

Proper way to check for string

Jun 19th, 2012
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.47 KB | None | 0 0
  1. // http://www.zachstronaut.com/posts/2009/02/09/careful-with-php-empty.html
  2.  
  3. // $mystring is NOT defined
  4. //$mystring = null;
  5. if (empty($mystring)) {
  6.     // the condition passes without a warning
  7. }
  8.  
  9. if ($mystring == null) {
  10.     // the condition passes WITH a warning
  11. }
  12.  
  13. if (!isset($mystring) || $mystring == null) {
  14.     // the condition passes without a warning
  15.     //
  16.     // this conditional is a suitable replacement for empty() when
  17.     // dealing with strings
  18. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement