Archive from December 2011
Value Checking in PHP with isset() and empty()

PHP has several functions which can be used to determine certain things about the value of a variable. None of these functions are particularly complex, but they can be difficult to tell apart – for instance, when to use empty() versus isset().

The PHP Doc site has a Type Comparison Table demonstrating the result you get from these comparison functions based on several different values. This table serves as a great reference to find out which function is designed to provide the kind of comparison you need.

For this post, I want to focus on three specific ways of checking values. Considering the following:

1
2
3
$nullvar;
$setvar = "we are set.";
$setbutfalse = false;

What happens when you run isset(), empty(), or if() on each of these variables? Here are some pointers:

isset()

  • isset() returns true if a variable is set, no matter what its contents. The contents may be: false, 0, ”, or anything else, and isset() will still return true. So long as the variable has had some value set, it will be true. There is one exception.
  • isset() returns false if a variable has never been set or if a variable has been assigned the specific keyword NULL (i.e., $var = NULL;).
  • For the above variables, isset($nullvar) will be false; isset($setvar) will be true; isset($setbutfalse) will be true.

empty()

  • empty() returns true for any variable that is not set, contains no value, or contains a value that equals zero (such as 0, “0″, false, etc).
  • For the above variables, empty($nullvar) will be true; empty($setvar) will be false; empty($setbutfalse) will be true.

if()

  • if($var) does essentially the opposite of empty($var). When checking a variable with if($var), it will return true if the variable is set and contains some value other than zero/false.
  • For the above variables, if ($nullvar) will be false; if ($setvar) will be true; if ($setbutfalse) will be false.

When to use which?
It depends on what you are trying to do.

  • If you want to see if a variable is set, period, and don’t care what its value is, use isset().
  • If you want to see if a variable is empty or contains a value that equals 0 or false, use empty().
  • If you want to see if a variable is not empty and does not contain a false/zero value, use !empty(). For example, if (!empty($passed)).
  • For the above example, you could also use if ($passed) and it would do the same thing.

Perhaps this post helps clear a little of the fog. Perhaps it just makes the fog thicker. Either way, it shows that PHP has a lot of little tools to meet a lot of use cases. In PHP, there are many ways to skin a cat. The trick is knowing which way is best.

Using Tippy Outside WordPress

Tippy has been written with WordPress in mind, but what if you want to use Tippy outside of WordPress – in a static HTML file, another CMS, or a custom project? In such situations, it will not be possible to use the Tippy short tags, but you can still use Tippy following the approach described in Using Tippy Without the Tags. To make it work, you will need to be sure and include any files needed by Tippy: jQuery, dom_tooltip.js, and dom_tooltip.factory.css.

I have put together an example: Tippy on a Static Page. Go there and view the page source to see how to set it up, or scroll around the code example below.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
<html>
    <head>
        <title>Static Tippy Example</title>
       
        <link rel="stylesheet" href="http://croberts.me/wp-content/plugins/tippy/dom_tooltip.factory.css" type="text/css" />
       
        <script src="http://croberts.me/wp-content/themes/croberts/js/jquery-1.6.1.min.js" type="text/javascript"></script>
        <script type="text/javascript" src="http://croberts.me/wp-content/plugins/tippy/dom_tooltip.js"></script>

        <script type="text/javascript">
            Tippy.tipPosition = "link";
            Tippy.tipOffsetX = 0;
            Tippy.tipOffsetY = 10;
            Tippy.fadeRate = 300;
            Tippy.sticky = false;
            Tippy.showClose = true;
        </script>
    </head>
   
    <body style="background-color: #ddd;">
        <div style="width: 700px; margin: 0 auto; background-color: white; border: 1px dashed #333; padding: 20px;">
            The purpose of this page is to show how you can use Tippy in a regular HTML file (or any other type of page) without having to go through WordPress. View the source of this page and note the files that must be included: dom_tooltip.factoy.css; jQuery; dom_tooltip.js. Also note the settings that must be initialized in the script section.<br /><br />
            Here is a <a class="tippy_link" tippyTitle="Tippy Link" id="mydiv" onmouseover="Tippy.loadTipInfo('Easy peasy.', 0, 0, 'mydiv', event);" onmouseout="Tippy.fadeTippyOut();">Tippy Link</a> set up in a standard HTML page.<br /><br />
           
            Below, see Tippy applied on an image:<br /><br />
           
            <a id="tippy_tipSample0" class="tippy_link" title="Tippy with an image" onmouseover="Tippy.loadTipInfo('You can put anything in your Tippy tooltips. Just be aware that things like &quot; will have to be converted to html entities. View source and look for this line for an example.<br /><br /><img src=&quot;http://upload.wikimedia.org/wikipedia/commons/thumb/c/c3/NGC_4414_%28NASA-med%29.jpg/291px-NGC_4414_%28NASA-med%29.jpg&quot; />', 0, 0, 'tippy_tipSample0', event);" onmouseout="Tippy.fadeTippyOut();"><img style="height: 200px;" src="http://croberts.me/wp-content/uploads/2011/05/giraffe.jpg" /></a>
        </div>
    </body>
</html>