<?php
/**
 * Converting (casting) PHP Variables to boolean
 * by Julius Beckmann http://juliusbeckmann.de
 * 23.07.2009
 */

echo "Converting (casting) PHP Variables to boolean\n";

function 
output($bool$txt='') {
    echo ((bool)
$bool) ? 'true ' 'false';
    echo 
' - '.$txt."\n";
}

// true / false / null
echo " - Booleans - \n";
output((bool)false'(bool)false'); // false
output((bool)null,  '(bool)null');  // false
output((bool)true,  '(bool)true');  // true

// Numbers
// Only 0 is false
echo " - Integer - \n";
output((bool)0,   '(bool)0');  // false
output((bool)1,   '(bool)1');  // true
output((bool)-1,  '(bool)-1'); // true

// Strings
// Only the empty String and the String 
// that will be casted to a 0 is false
echo " - Strings - \n";
output((bool)'',    '(bool)\'\'');   // false
output((bool)'0',   '(bool)\'0\'');  // false
output((bool)'1',   '(bool)\'1\'');  // true
output((bool)'-1',  '(bool)\'-1\''); // true
output((bool)'a',   '(bool)\'a\'');  // true
output((bool)"a",   '(bool)"a"');    // true
output((bool)"\n",  '(bool)"\n"');   // true
output((bool)"\0",  '(bool)"\0"');   // true

// Arrays
// Only Arrays with a count() of 0 are false
echo " - Arrays - \n";
output((bool)array(),        '(bool)array()');         // false
output((bool)array(''),      '(bool)array(\'\')');     // true
output((bool)array('0'),     '(bool)array(\'0\')');    // true
output((bool)array('a'),     '(bool)array(\'a\')');    // true
output((bool)array(array()), '(bool)array(array())');  // true

// Objects
// Every object will be casted to true, 
// even without methods or values inside.
echo " - Objects - \n";
class 
nothing { };
$nothing = new nothing;
output((bool)$nothing,  '(bool)$nothing');  // true
$nothing->this 'that';
output((bool)$nothing,  '(bool)$nothing');  // true

?>