Re: foo[bar] _or_ foo['bar'] ?
| From: | php3 at developersdesk dot com | Date: | Tue, 13 Feb 2001 20:02:54 +0000 |
| Subject: | Re: foo[bar] _or_ foo['bar'] ? | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-39470@lists.php.net to get a copy of this message | ||
Addressed to: Derek Sivers <list@hitmedia.com>
"Matthias Krehl" <matthias.krehl@key-work.de>
php-general@lists.php.net
** Reply to note from Derek Sivers <list@hitmedia.com> Tue, 13 Feb 2001 09:31:29 -0800
>
>
> >I'd really appreciate a clear statement whether to use better
> > $foo = array('bar' => 'boing');
> > doWhatSoEver($foo[bar]);
> >or
> > $foo = array('bar' => 'boing');
> > doWhatSoEver($foo['bar']);
>
>
> I had heard someone on the list say that it's always best to use quotes,
> for the same reason as XHTML, where every value MUST have quotes.
>
> But honestly I've been using only NO-quotes all year and find it much
> easier to read. And NEVER any problem!
>
> Also makes it easier when you need to use an array variable inside a quoted
> string:
>
> print "Hello there $client[name]";
Several people have mentioned that this is a bad practice, but no one
has mentioned why...
There are two gotchas you have to look out for, that quotes will
prevent. First, if you are using some_word as an array index and
someone decides that it would be a good name for a built in function,
constant, or statement all of a sudden all of your programs will break.
Second, take one of your programs that uses some_word all over it, and
add:
define( 'some_word', 'something_else' );
into one of your include files, now life gets very interesting:
$X[ 'some_word' ] = 'foo';
$X[ 'something_else' ] = 'bar';
echo $X[ some_word ]; // prints bar
echo $X[ 'some_word' ]; // always works as expected.
Rick Widmer
Internet Marketing Specialists
http://www.developersdesk.com