Skip navigation.
Home

More solutions for idle CPU time.

Wonky Code

Yesterday we had a great example of how to make sure your database isn't slacking off. Here's a example to make sure your web server keeps working hard too.

Also thanks to everybody who submitted a WTF. Keep them coming! :)

<?php
if( $_USER['uid'] > 1 )
    {
        
$usermenu = new Template( $_CONF['path_layout'] );
        
$usermenu->set_file( array( 'option' => 'useroption.thtml',
                                    
'current' => 'useroption_off.thtml' ));
        
$usermenu->set_var( 'site_url', $_CONF['site_url'] );
        
$usermenu->set_var( 'layout_url', $_CONF['layout_url'] );
        <
strong>$usermenu->set_var( 'block_name', str_replace( '_', '-', 'user_block' ));</strong>

        if( empty(
$title ))
        {
            
$title = DB_getItem( $_TABLES['blocks'], 'title', "name='user_block'" );
        }
    
//....
?>

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

While not as extreme as yesterdays, it still begs the question, why? Why call a replace function on a hard-coded string?? The only explanation I think of is that the literal string used to be a variable and when it became a literal, the person that made the code change didn't strip out the function. Perhaps it was the victim of a global search & replace?

That's how you make your programs look more complicated than they are. :)

I've seen the light! From now on, I will always write code like the following:


Oh great, it ate the beautiful PHP code I wrote.

Here we go again!


message$ = 'Hey loser, pissing us off again?';
message$ = str_replace('off', 'you');
message$ = str_replace('loser', 'Dear Visitor');
message$ = str_replace('?', '.');
message$ = str_replace('us', 'see');
message$ = str_replace('Hey', 'Hello,');
message$ = str_replace('pissing', 'it's nice to');
echo message$;

Oh man, what happened to all those dollar signs? O_o

Anyways, here is a little improvement. Where it says:

echo $message;

It should say:

echo "$message";

It's too easy for the server to just print the variable. It must do variable substitution!

P.S.: I've seen lots of echo instructions like the latter in code written by beginners. ^_^

I've done worse.

I remember looking back at some old code of mine from when I was just learning php, and finding the following.


html here...

more html...

Aaarrrgh


html here...
{?php echo "foo"; ?}
more html...


Imagine the curly braces are the normal angle brackets.

Maybe it's a bug and the line should be
$usermenu->set_var( 'block_name', str_replace( '_', '-', $_CONF['user_block'] ));
instead of the above.

Maybe he is trying for consistency in a weird way. Perhaps he has to integrate back-end variable names that contain dashes instead of underscores. For consistency, he uses underscores everywhere so that search/replace still works and he doesn't have to worry about making a change in he code that won't get reflected in the backend and vice-versa.

Post new comment




*

  • Web and e-mail addresses are automatically converted into links.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd> <pre> <p> <br /> <br>