There’s bad code, and then there’s code so bad that you only need to see one line of code to understand how bad it actually is. Simon supplied this tiny horror which manages to combine all that’s wrong with PHP with the worst of loose typing and a thick layer of not really understanding what you’re doing.

$dtRecord->setProcessing((boolean)'true');
Korean Traffic sign (Pass Left or Right)

PHP is a terrible language, but it’s not so terrible that it doesn’t have a boolean type. It’s perfectly happy to mangle juggle your types, and for that reason, it actually discourages the use of any sort of type casting.

Yes, by PHP standards, casting types is an anti-pattern, but that’s not why this is as much of a WTF as it is.

PHP lets you use anything as a boolean, and follows the same general conventions as other type-mangling languages- the integer 0, the floating point value 0.0, an empty string (or the string “0”) are all false.

So, now we’re stacking up the WTFs- the original programmer could have just passed TRUE, instead of “true”, but really, it didn’t matter what they passed, so this line probably would have made just as much sense:

$dtRecord->setProcessing((boolean)'WTF');

I fortunately don’t work with PHP, so I had to do a little fact checking to verify that this line was as stupid as I suspected it was, and that meant reading up on PHP’s handling of booleans and their odd results. At the risk of picking on PHP, while I wanted to understand how it did booleans, I discovered that it has two different boolean operators- “OR” and “||”, which isn’t a WTF, and that they have totally different orders of operation.

As a result, you run into weird cases like this:

$x=TRUE;
$y=FALSE;
$z= $y OR $x;

$z is false at the end of that expression, because the assignment is actually evaluated before the “OR”. Essentially, the final line is actually:

($z=$y) OR $x;

The “||” operator works like a normal person would expect it to.



[Advertisement] BuildMaster allows you to create a self-service release management platform that allows different teams to manage their applications. Explore how!