The Pain of Types in PHP

The only way to check an uncast scalar variable in PHP for being truly non-empty (i.e., not being null or an empty string (''), but allowing any valid boolean, integer, float, string, or decimal, is:

'' !== $x && null !== $x

A caveat to this is you’ll have to detect arrays separately, because a garbage array could be array(), array(''), array(null), or even array('', ''). At some level you just have to accept that as long as you document what a method or function expects, whoever uses it will have to check that stuff themselves or expect garbage results.

In a strongly typed language, all this would be detected at compile time, but then again your development time would be longer and you couldn’t grab just any newbie off the street and throw them at it.

Unfortunately, newbies off the street generate garbage, so I end up either wanting an expensive (in terms of processing time) function that checks if anything I get is either a non-empty scalar or a non empty array of non-empty scalars (or arrays that meet the same criteria). Simpler just to have strong types that will produce a nice, ugly error if your function expects an integer and you pass it a string.