User Tools

Site Tools


docs:tips_n_tricks:php.html

This is an old revision of the document!


PHP

Meaning of isset() and empty()

<?

  print '| $foobar       ';
  $foobar = NULL;
  if(isset($foobar)) print '| set       ' ;
  else               print '| not set   ' ;
  if(empty($foobar)) print '| empty     ' ;
  else               print '| not empty ' ;
  print "|\n";

  print '| $foobar = 0   ';
  $foobar = 0;
  if(isset($foobar)) print '| set       ' ;
  else               print '| not set   ' ;
  if(empty($foobar)) print '| empty     ' ;
  else               print '| not empty ' ;
  print "|\n";

  print '| $foobar = ""  ';
  $foobar = "";
  if(isset($foobar)) print '| set       ' ;
  else               print '| not set   ' ;
  if(empty($foobar)) print '| empty     ' ;
  else               print '| not empty ' ;
  print "|\n";
  print '| $foobar = 42  ';
  $foobar = 42;
  if(isset($foobar)) print '| set       ' ;
  else               print '| not set   ' ;
  if(empty($foobar)) print '| empty     ' ;
  else               print '| not empty ' ;
  print "|\n";

  print '| $foobar = "42"';
  $foobar = "42";
  if(isset($foobar)) print '| set       ' ;
  else               print '| not set   ' ;
  if(empty($foobar)) print '| empty     ' ;
  else               print '| not empty ' ;
  print "|\n";

  print '| $foobar = "x" ';
  $foobar = "x";
  if(isset($foobar)) print '| set       ' ;
  else               print '| not set   ' ;
  if(empty($foobar)) print '| empty     ' ;
  else               print '| not empty ' ;
  print "|\n";

  print '| $foobar = NULL';
  $foobar = NULL;
  if(isset($foobar)) print '| set       ' ;
  else               print '| not set   ' ;
  if(empty($foobar)) print '| empty     ' ;
  else               print '| not empty ' ;
  print "|\n";

gives

| $foobar       | not set   | empty     |
| $foobar = 0   | set       | empty     |
| $foobar = ""  | set       | empty     |
| $foobar = 42  | set       | not empty |
| $foobar = "42"| set       | not empty |
| $foobar = "x" | set       | not empty |
| $foobar = NULL| not set   | empty     |
docs/tips_n_tricks/php.html.1331196686.txt.gz · Last modified: 08.03.2012 09:51 CET by peter

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki