One thing I love from Rails is the ability to do a .blank? method on any object. This is infinitely useful when you can’t be sure what type of value a variable is, or if it can potentially be different types. This is something that I always miss in PHP. Indeed since form data always comes back as strings and different ORM’s and database connectors (PDO, Propel, Doctrine, mysqli, PEAR DB, PEAR MDB2, etc) return different types, having a generic “is this value essentially blank, null, false, empty” is important.
A while back I set out to try and duplicate this functionality in a PHP function. Over the course of the past few months I feel it’s slowly matured and become something extremely valuable that I can’t live without.
It returns true when ever it is sure a value is blank, and false otherwise. It considers NULL, and false to be blank, and true to be not blank at the start. It then considers integers of 0 to be blank, and floats of 0.0 to be blank. It considers any string that is only composed of blank space characters, or “0”’s to be blank. I have considered adding a string of “false” or “null” but for now I considered this not to be generalizable enough. I feel that a string just of “0”’s almost always counts as a blank value when I’m processing data, but I could possibly see a string of “false” or “null” representing a non-blank value.
Then we get to the difficult part, arrays. The function of course regards an empty array as blank, but it will also recursively (this is performed by calling its self) go through all elements and if they are all blank declare the whole array blank.
There is also the special case of Objects that developers wish to be considered enumerable/iterator/array-like objects. These are created by implementing the SPL iterator interfaces. So the function checks to see if the necessary interfaces are implemented, and if they are treats the object, exactly like it would an array.
The last rule is it considers all other objects to be not blank.
This function can of course be expanded or refined to meet your particular needs. If, for instance, you have a self-defined Null object that you use you get set that to return true. Or if you don’t want strings with just “0” in them to return true, you can change that. The function is very simple and it should be obvious where you change things to get the result you want.
Here’s the code:
function is_blank($value){ $type = gettype($value); switch ($type) { case 'NULL': return true; break; case 'boolean': if ($value === false) return true; break; case 'integer': if ($value == 0) return true; break; case 'double': if ($value == 0.0) return true; break; case 'string': if (preg_match('/^\s*0*\s*$/', $value)) return true; break; case 'array': if (count($value) == 0) { return true; } else { $all_blank = true; foreach ($value as $item) { if (!is_blank($item)){ $all_blank = false; } } if ($all_blank){ return true; } else { return false; } } break; case 'object': $implements = class_implements($value); if ( in_array('ArrayAccess', $implements) && ( in_array('IteratorAggregate', $implements) || in_array('Iterator', $implements) || in_array('SeekableIterator', $implements) || in_array('RecursiveIterator', $implements) ) && in_array('Countable', $implements) ) { if (count($value) == 0) { return true; } else { $all_blank = true; foreach ($value as $item) { if (!is_blank($item)){ $all_blank = false; } } if ($all_blank){ return true; } else { return false; } } } break; default: return false; break; } return false; }
Pastie: http://pastie.textmate.org/169940
Pastie code download: http://pastie.textmate.org/pastes/169940/download



