1

I am in the process of creating an associative array called errMsgs[] that will hold error messages that correspond to a particular <input> field error. Which form of testing errMsgs[] is more correct, faster, and safer? Does either method have important advantages or disadvantages?

Contact Form Code Snippet: Focus --> The third <td></td> in a table with three columns as in ...

(column1)    (column2)     (column3)  <----Focus of this code snippet.
---------    ----------    ----------

Where column1 is a <label></label> inside of a <td></td>. Where column2 is an <input /> control inside of a <td></td>. Where column3 is an empty <td></td>, filled conditionally based on the errMsgs[] associative array.

Skeleton 1

    <tr>
        <td>
            <label>
            </label>
        </td>
        <td>
            <input />
        </td>
        <td>
<?php
            if($errMsgs['firstName'])
            {
                echo $errMsgs['firstName']; //This element is not from user input. 
            }
?>
        </td>
    </tr>

Skeleton 2

    <tr>
        <td>
            <label>
            </label>
        </td>
        <td>
            <input />
        </td>
        <td>
<?php
            if(isset($errMsgs['firstName']))
            {
                echo $errMsgs['firstName'];  //This element is not from user input.
            }
?>
        </td>
    </tr>

Escaping and coding style issues aside, which skeleton is superior? Which have you used in the past? I want to use the best way.

BoltClock
  • 700,868
  • 160
  • 1,392
  • 1,356
Anthony Rutledge
  • 6,980
  • 2
  • 39
  • 44
  • 1
    `isset` is the correct way to do this, using just `if($errMsgs['firstName'])` will throw an `Undefined index` notice. (Your examples are missing the `$` sign btw.) And speed is no concern for such a minimal operation anyway. – CBroe May 31 '14 at 19:29
  • `if($var)` would throw a notice if the variable is not defined in the first place, whereas `if(isset($var))` would not. I'd personally use `!empty($var)`, though. – Amal Murali May 31 '14 at 19:30
  • @CBRoe Thanks, I was stuck in bash mode for a moment! – Anthony Rutledge May 31 '14 at 19:35
  • @Amal Murali. I think empty() permits too many possibilities compared to isset(). If the number zero is the element, I do not want that to evaluate to FALSE. – Anthony Rutledge May 31 '14 at 19:36
  • [The Definitive Guide To PHP's isset And empty](http://kunststube.net/isset/) – deceze May 31 '14 at 20:49
  • @deceze Empty is not even in the running here. It's too broad and not the right way to do it. isset() is a language construct, not a function. – Anthony Rutledge May 31 '14 at 21:07
  • An empty array already returns false inside of if() – Anthony Rutledge May 31 '14 at 21:12
  • 1
    I love how people are always grouping the terms "more correct", "faster", and "safer" together when asking comparison questions as if the three concepts went hand-in-hand. – BoltClock Jun 01 '14 at 04:22
  • I posted the link to that article because it explains in detail what `isset` does and when it should be used. Yes, it also covers `empty`, can't hurt you to learn about that too. – deceze Jun 01 '14 at 07:46
  • I'm sure it can't hurt someone to learn about it. Good point. – Anthony Rutledge Jun 01 '14 at 12:59

2 Answers2

2

You could try it.

if($errMsgs['firstName']) will return a notice when the key firstName doesn't exist. So use isset() instead.

Amal Murali
  • 75,622
  • 18
  • 128
  • 150
pavel
  • 26,538
  • 10
  • 45
  • 61
  • Ah, thanks. I have been doing this on my white board. White boards tend not to give error messages! – Anthony Rutledge May 31 '14 at 19:38
  • @AnthonyRutledge: I don't know what a white board is, but if you're talking about PHP, you can [enable error reporting](http://stackoverflow.com/a/6575502/1438393) to have these errors reported. – Amal Murali Jun 01 '14 at 03:47
  • @AmalMurali A white board is short for a dry erase marker board. I am aware of error reporting. I used it in the development versions of my php.ini. – Anthony Rutledge Jun 01 '14 at 03:51
  • @AnthonyRutledge: Note that it's also possible to the same thing with `array_key_exists()` - might be more readable when dealing with arrays :) – Amal Murali Jun 01 '14 at 03:55
  • @AmalMurali That would probably be a more expensive way to do it. `isset()` is a language construct, not a function. – Anthony Rutledge Jun 15 '14 at 14:36
1

Good question. The language construct isset() is the way to go here.

Melinda
  • 76
  • 8