In doing more WordPress work, I ran across more bad code in a theme. Bad code was a reoccurring theme in this theme.
global $options; foreach ($options as $value) { if (get_settings( $value['id'] ) === FALSE) { $$value['id'] = $value['std']; } else { $$value['id'] = get_settings( $value['id'] ); } }
First off, isn’t that annoying, having to scroll? Making an if-else into a one-liner doesn’t save the computer any time processing it and costs humans significantly more
global $options; foreach ($options as $value) { if (get_settings( $value['id'] ) === FALSE) { $$value['id'] = $value['std']; } else { $$value['id'] = get_settings( $value['id'] ); } }
There, that’s nicer. Now secondly, globals are generally a bad idea, especially something so generic as options. This snippet gets all options into variables that are created on the fly using the $$ operator.
Thirdly, $$ operator? FTW operator? I know, I had to look it up too. Guess what? The PHP manual itself recommends against using it because of security considerations. There is no need to get this complicated to provide defaults for unset global options.
I’d explain what the $$ it actually does, but it might encourage people to use it. Let’s just say it’s a code mechanism that should be reserved for much better languages than PHP. I’m also discouraged to explain because I’ve discovered bonus bad code discovered while writing this very post! I was pasting the above code in and had to add a pre tag with lang=”php” as the attribute. TinyMCE added this attribute to all later p tags as well. Why? It does this with no warning.
I actually had even more bad PHP code to complain about, but I’ll save it for another post. Right now I’ll just complain about how I have to copy the entire raw HTHML in WP’s editor before I switch back to visual because I never know what it’s going to decide to randomly delete, mangle, or mutilate.