depends on what
exactly you're trying to check. I've heard empty is Bad(tm), since it evaluates to true even for empty arrays.
Personally, I use array_key_exists if I want to check that a variable actually is
in the array, no matter the value. Then I use strlen() > 0 to determine if it has any meaningful value. But mostly, the strlen() > 0 check is enough. It tells you that the value in $_POST contains something else than
I would avoid direct bool casting, like this:
since this evaluates to false in all of these cases:
- the value is not in array,
- the value is null,
- the value is an empty string '',
- the value holds an integer zero 0,
- the value holds a string zero '0',
which may not be exactly what you want. Run this code, it will give you an idea and then you will easily decide. Although I do think strlen() is the best possible option

.
Code: Select all
$keys = array('none', 'null', 'empty', 'zero', 'zerostr', 'emptyarray');
$array = array('null' => null, 'empty' => '', 'zero' => 0, 'zerostr' => '0', 'emptyarray' => array());
echo "<pre>";
print_r($array);
echo "\n* array_key_exists:<br />";
foreach ($keys as $key)
echo $key ." ... " . array_key_exists($key, $array) . "\n";
echo "\n* isset:<br />";
foreach ($keys as $key)
echo $key ." ... " . isset($array[$key]) . "\n";
echo "\n* bool cast:<br />";
foreach ($keys as $key)
echo $key ." ... " . (bool)($array[$key]) . "\n";
echo "\n* strlen() > 0:<br />";
foreach ($keys as $key)
echo $key ." ... " . (strlen($array[$key]) > 0) . "\n";
echo "\n* !empty():<br />";
foreach ($keys as $key)
echo $key ." ... " . (!empty($array[$key])) . "\n";
echo "</pre>";