docs:tips_n_tricks:php.html
PHP
Meaning of isset() and empty()
~ $ php --version PHP 5.3.8 (cli) (built: Dec 5 2011 21:24:09) Copyright (c) 1997-2011 The PHP Group Zend Engine v2.3.0, Copyright (c) 1998-2011 Zend Technologies
<? print "PHP Version is ".phpversion()."\n"; print '+---------------'; print '+-----------' ; print '+-----------' ; print "+\n"; print '| value '; print '| isset() ' ; print '| empty() ' ; print "|\n"; print '+---------------'; print '+-----------' ; print '+-----------' ; print "+\n"; 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"; print '+---------------'; print '+-----------' ; print '+-----------' ; print "+\n";
or
<? print "PHP Version is ".phpversion()."\n"; print '+-------------------'; print '+-----------' ; print '+-----------' ; print "+\n"; print '| value '; print '| isset() ' ; print '| empty() ' ; print "|\n"; print '+-------------------'; print '+-----------' ; print '+-----------' ; print "+\n"; printf('| $foobar %-7s | %-9s | %-9s |'."\n",'' ,(isset($foobar) ? '' : 'not ').'set',(empty($foobar) ? '' : 'not ').'empty' ); $foobar = 0; printf('| $foobar = %-7s | %-9s | %-9s |'."\n",'0' ,(isset($foobar) ? '' : 'not ').'set',(empty($foobar) ? '' : 'not ').'empty' ); $foobar = ""; printf('| $foobar = %-7s | %-9s | %-9s |'."\n",'""' ,(isset($foobar) ? '' : 'not ').'set',(empty($foobar) ? '' : 'not ').'empty' ); $foobar = 42; printf('| $foobar = %-7s | %-9s | %-9s |'."\n",'42' ,(isset($foobar) ? '' : 'not ').'set',(empty($foobar) ? '' : 'not ').'empty' ); $foobar = "42"; printf('| $foobar = %-7s | %-9s | %-9s |'."\n",'"42"' ,(isset($foobar) ? '' : 'not ').'set',(empty($foobar) ? '' : 'not ').'empty' ); $foobar = "x"; printf('| $foobar = %-7s | %-9s | %-9s |'."\n",'"x"' ,(isset($foobar) ? '' : 'not ').'set',(empty($foobar) ? '' : 'not ').'empty' ); $foobar = array(); printf('| $foobar = %-7s | %-9s | %-9s |'."\n",'array()',(isset($foobar) ? '' : 'not ').'set',(empty($foobar) ? '' : 'not ').'empty' ); $foobar = NULL; printf('| $foobar = %-7s | %-9s | %-9s |'."\n",'NULL' ,(isset($foobar) ? '' : 'not ').'set',(empty($foobar) ? '' : 'not ').'empty' ); print '+-------------------'; print '+-----------' ; print '+-----------' ; print "+\n";
gives
PHP Version is 5.3.8 +---------------+-----------+-----------+ | value | isset() | empty() | +---------------+-----------+-----------+ | $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.txt · Last modified: 08.03.2012 10:23 CET by peter