CSI: PHP

"Looking at your tweets I cannot even fathom what your job is. CSI:PHP?" — @grmpyprogrammer

Artisan Level Code Trolling

| Comments

WARNING: The code you’re about to view is intended for mature audiences and may not be suitable for all readers.

I saw this horrifying code snippet in my Twitter feed this afternoon.

Tweet could not be processed

To make this abomination easier to read, I’ve copied and formatted it here.

1
2
3
4
<?
foreach (get_defined_vars() as $var) {
    @extract($var);
}

What this nightmare does, in case you aren’t immediately able to grok it, is iterate over a multidimensional array containing a list of all defined variables (environment, server, and user defined) and import them into the current symbol table. If that weren’t bad enough, it uses the @ error control operator and (because this is extract’s default behavior) overwrites any existing defined variable it encounters.

Try and debug unexpected behavior with that in your code. Go ahead. Try. You’ll be reduced to a wimpering pile of crazy in just a few short hours.

There simply aren’t words sufficient to express the horror of this snippet. The worst thing about this is I’ve seen similar code in the wild, and so has Graham.

Tweet could not be processed

Looks like osCommerce might have some ‘splainin’ to do …

It’s a cold, cruel world of bad code sometimes. I’m sorry I had to share this with you. Hopefully it helps more than it hurts, since knowing what bad code looks like is one of the first steps in being able to fix it.

Comments