Baby, Don't You Loose Your Type On Me

Many programmers from beyond the realm of PHP, deep in the black country where the dragons reside, whinge like an Australian swim-coach about PHP's loose variable typing. Many PHP developers either don't know what this means or don't care. I didn't much care, until recently, where I got a practical lesson in why loose typing can, on ocassion, really suck.

Here are some interesting effects of PHP's loose typing:

$var1 = 1;
$var2 = '1';
print 'Result: '.($var1 == $var2);
?>

And it prints 1 (in other words true) even though the first variable is an integer and the second a string. Here's another interesting one:

$var1 = 0;
$var2 = false;
print 'Result: '.($var1 == $var2);
?>

True again! Even though the first variable is clearly an integer and the second is clearly a boolean.

So you can (and I would argue you always should, unless there's a really good reason not to) enforce strict typing when doing all variable comparisons, avoiding lazy options such as !$variable to indicate a boolean false.

The other thing you should do, though no PHP developer (myself included) does, is properly declare and type your variables before you need them, for example:

$flag = (bool) true;
?>

If you set error_reporting to E_ALL in your php.ini file then PHP will issue a notice message if you don't declare variables properly and just try and compare them on the off chance they were created somewhere (a common technique with PHP). There's a good note on it here:http://p2p.wrox.com/topic.asp?TOPIC_ID=7613

Drupal wants to head towards being E_ALL compliant, so getting in to some good habits would be well worthwhile, unless you want to have to hack at the default .htaccess file to prevent all the warnings on your page caused by your sloppyness! Or fix them (heaven forbid).

We’re Code Enigma

We’re one of the most experienced Drupal teams in Europe, best known for our work on large, technically challenging projects for all kinds of clients.

Our team is passionate about Drupal and open source software. Our whole company spends at least four weeks per year working on Drupal modules or other open source projects. We’re also strongly committed to putting design first, taking a mobile-first, content-out approach to creating websites. This ensures that the sites we build combine the power of Drupal with best practice design and development.