Menu

#2 missing quotes in array key names

open
nobody
None
5
2004-07-28
2004-07-28
Anonymous
No

Sites using the E_ALL error_reporting setting will see
a whole bunch of warnings due to the use of unquoted
key names in your array definitions.

for example:

$array_name[business]="$_POST[business]";

should be

$array_name['business']="$_POST[business]";

Also, it wopuld speed up the script a bit if your did
not use the double quote around while assigning as well.

This would make the above read as follows:

$array_name['business'] = $_POST['business'];

And then, to eliviate some of the memory issues in most
PHP scripts today, you could assign the variables by
reference.

$array_name['business'] =& $_POST['business'];

This would make much more sense.

Discussion

  • Jon

    Jon - 2004-09-27

    Logged In: YES
    user_id=784392

    i've seen =& used before, but i haven't seen much
    documentation on it. do you have any doc links, or would you
    like to post an explanation for my benefit?

     
  • Marcus Bointon

    Marcus Bointon - 2004-11-17

    Logged In: YES
    user_id=602594

    By default, PHP4 makes copies of arrays and objects when used this way,
    which is wasteful of time, CPU and memory. Using & tells PHP to assign
    the value by reference, i.e. to point the new variable at the same chunk
    of memory as the original, which saves copying the actual data. I think
    you'd best ignore it for now - it offers no advantage in PHP5 (which uses
    references by default) and syntax is cleaner without it.

    The official docs on references are here:

    http://www.php.net/manual/en/language.references.php

    Aside from that, comments in this report are all correct - please don't
    use unquoted array indices like this - it's just so PHP3... ;^)

     

Log in to post a comment.