Hi guys.

I just need to ask about the isset() function.

It seems to me like it is a pretty pointless function.

It is used in conjunction with form fields, to check they are set, but it still returns true even if the form fields are empty.

What is the point in this function?

Paul.

    As mentioned in a previous reply, unchecked checkboxes and unchecked radio buttons are not set when the form has been submitted. You would use isset() to check these type of fields, to prevent generating php errors when they are not set.

    The point of isset() is to test for variables that might not exist/are optional. Another case where it would be used is to test if specific $_GET parameters are set or not in the request for a page.

      As pbismad says, isset() checks whether or not a variable exists. An empty variable exists, it just doesn't have a value. This is why isset() will return true on an empty variable - use empty() if you're checking to see if a value has been set in a variable. Even then, however, realize that a variable with a value of 0 is considered empty.

        Test it yourself...it's a great way to really learn things. 🙂

        <?php
        $test = array(
            '',
            'x',
            0,
            0.0,
            1.123,
            '0',
            false,
            true,
            null
        );
        echo "Test Data:".PHP_EOL.var_export($test,1).PHP_EOL;
        for($i = 0; $i <= count($test); $i++) // intentionally going 1 past end of array
        {
            $set = isset($test[$i]);
            $empty = empty($test[$i]);
            echo "$i - isset: ".var_export($set, 1).", empty: ".var_export($empty, 1).PHP_EOL;
        }
        

        Output:

        09:45 $ php isset_or_empty.php
        Test Data:
        array (
          0 => '',
          1 => 'x',
          2 => 0,
          3 => 0,
          4 => 1.123,
          5 => '0',
          6 => false,
          7 => true,
          8 => NULL,
        )
        0 - isset: true, empty: true
        1 - isset: true, empty: false
        2 - isset: true, empty: true
        3 - isset: true, empty: true
        4 - isset: true, empty: false
        5 - isset: true, empty: true
        6 - isset: true, empty: true
        7 - isset: true, empty: false
        8 - isset: false, empty: true
        9 - isset: false, empty: true
        
          5 days later

          The other posts here are good, but I would also point out that you get an error message when you try to check a variable that has not been defined:

          if ($this_var_has_not_been_defined) {
            echo "TRUE\n";
          } else {
            echo "FALSE\n";
          }
          

          the output of this script is:

          PHP Notice:  Undefined variable: this_var_has_not_been_defined in /tmp/foo.php on line 3
          PHP Stack trace:
          PHP   1. {main}() /tmp/foo.php:0
          FALSE
          

          With [man]isset[/man], you can check to see if a variable has been set first before trying to check its value. That way, you can avoid the error message.

            sneakyimp;11063359 wrote:

            The other posts here are good, but I would also point out that you get an error message when you try to check a variable that has not been defined:

            if ($this_var_has_not_been_defined) {
              echo "TRUE\n";
            } else {
              echo "FALSE\n";
            }
            

            the output of this script is:

            PHP Notice:  Undefined variable: this_var_has_not_been_defined in /tmp/foo.php on line 3
            PHP Stack trace:
            PHP   1. {main}() /tmp/foo.php:0
            FALSE
            

            With [man]isset[/man], you can check to see if a variable has been set first before trying to check its value. That way, you can avoid the error message.

            Of course, you can also avoid error messages by configuring the server not to show or log them ...

            It might be interesting to know if (and I think I know the answer) it's more expensive to write isset() and have the system check isset() than to allow the server to log all those errors. Maybe the best strategy for a production system is to ensure you're not logging E_ALL or E_STRICT? Isn't that a typical piece of advice?

              Write a Reply...