CSI: PHP

"Looking at your tweets I cannot even fathom what your job is. CSI:PHP?" — @grmpyprogrammer

How Not to Set a Variable

| Comments

This is the wrong way to set a variable …

1
2
3
4
5
6
<?php
foreach ($_POST as $Key => $Value) {
    // . . . 
    $values .= "'$Value',";
    // . . .
}

… unless you really like unexpected behavior and notice messages. This particular app throws notice after notice after notice. It throws so many, in fact, that the vertical scroll bar on many pages is just a tiny little sliver for all the Xdebug output.

Pro Tip: develop with error reporting turned way, way up. (I usually use error_reporting(-1) in development.)

UPDATE: I updated to code sample based on Dave’s comment that I may have introduced a bug during copy and paste. Indeed I had. Pop quiz: What do you think happens to $values after the foreach() exits?

Comments