Daniel Pataki

Web Developer

Improper checking for variables and values is a very frequent error I see among WordPress plugin and theme developers. While their products work fine they produce many PHP notices which will not bring the World to its knees but is an annoyance at the very least (and sloppy work). In less obvious cases it can lead to bugs and weird behavior.

Sending you home with a note

if ( $result ) {
    echo 'Your display named has been changed to ' . $result['display_name'];
}

So what’s the problem here? The above will throw a notice if $result is not defined. If notices are not shown this works as intended but is still sloppy. The reason it works is that when a variable is not set it evaluates to false. Hence the statement is echoed above if $result is set otherwise nothing happens.

Empty Inside

Similarly as above an unset variable is not the same as an empty variable so the below code will also throw a notice since we are trying to see if an unset variable is empty.

if ( empty( $result ) ) {
    echo 'Your display named could not be changed';
}

The proper way to do this

if ( isset( $result ) AND !empty( $result ) ) {
    echo 'Your display named has been changed to ' . $result['display_name'];
}

When Sloppiness Leads To Bugs

Say you’re creating a WordPress theme and you want the user to be able to specify a logo. If no logo is specified the site title is shown in heading text.

$logo = get_option( 'my_site_logo' );
if ( isset( $logo ) ) {
    echo '<img src="<?php echo $logo ?>">';
}
else {
    echo '<h1>' . get_bloginfo() . '</h1>';
}

So let’s see what a user would do/see. He downloads the theme, activates it and sees his new website with the website title in a nice heading on top. He then decides to use a logo instad so pastes the URL of an image he uploaded. The image shows up, the user is awesome happy and all is well.

Once day he decides to switch back to the text again until all is well so he removes the URL from the input box. He goes back to his where there is now no logo and no title either. He is furious and eats his own arm in sorrow.

In this case the ‘my_site_logo’ option is still in the database, it just has an empty value. So when you pull it $logo will be set as if you would have hard coded it like so: $logo = ''. When we get to the if statement $logo is set so the image is output but the source will be empty.

To prevent this you should always use isset() and empty() in conjunction.

$logo = get_option( 'my_site_logo' );
if ( isset( $logo ) AND !empty( $logo ) ) {
    echo '<img src="<?php echo $logo ?>">';
}
else {
    echo '<h1>' . get_bloginfo() . '</h1>';
}

Don’t Trust Yourself

Many programmers’ first reaction is that this is stupid. When saving the option we always check to make sure the value isn’t empty. If it is we delete the option from the database which means that the emptiness check isn’t necessary.

While this is true this is just bad practice and you are opening yourself up to bugs. Even with the best coding practices we get so many bugs it’s a shame to have to spend time fixing ones we cluld have prevented easily.

  1. Gabriele Romanato

    Really interesting points. As a side note, there’s also the self-inflicted problem of how some PHP functions are explained to the community. PHP’s functions are generally misleading both in their name and general purpose. Reading other developer’s comments on php.net is something that I warmly recommend to make things clear about a particular feature of the language. A typical example are the exceptions raised by the ctype-x function family when particular situations occur. Thanks for sharing your ideas. :-)

  2. AK Ted

    Thanks for the article. All of us developers should *always* code with error reporting enabled. It’s obvious from trying out many plugins and themes in WordPress that too many developers do not.

    One error in your post, though, regarding empty(). It doesn’t throw an error with an undefined var. According to the PHP.net manual entry for empty()(http://php.net/manual/en/function.empty.php):

    “No warning is generated if the variable does not exist. That means empty() is essentially the concise equivalent to !isset($var) || $var == false.”

Leave a Reply