|
Posted by Richard Levasseur on 11/18/73 11:42
The note here:
http://us2.php.net/manual/en/ref.errorfunc.php
still says
echo "$arr[foo]"
will give an E_NOTICE (it doesn't say if it means within the context of
a double quoted string or not, but past history would imply it does, as
I specifcally remember seeing E_NOTICE errors for this when I
originally began learning php. I guess it's context has changed, it'd
be nice if that was explicit considering its history), however then
link you gave,
http://www.php.net/manual/en/language.types.string.php#AEN3054 ,
doesn't mention that, so i guess the $arr[foo] not giving an E_NOTICE
is an intended change and is a problem with the documentation. (or its
unintended and the documentation is wrong...haha)
Aside: That page still says using {} for string offsets is preferred,
however, I remember reading php meeting notes that {} was going to be
deprecated (removed in php 6) and [] was going to be the preferred
method. http://www.php.net/~derick/meeting-notes.html#cleanup-for-vs
Manual contradictions can be fun! :)
Speed of executation: a friend of mine ran some tests comparing $arr,
$arr["index"], $arr[index], $arr[$index], $arr['index'], numerical
indices vs string indices, with and without {}. It was a few years ago
(php4) and was just an informal test to see what would happen. Results
may have changed, but anyways:
In general, using {} was faster than not using {}, ({$foo} vs $foo).
Numerical indices ($arr[0], not $arr['0'] or $arr["0"]) were faster
than string indices.
Single quotes and variables ($arr['index'] vs $arr[$index]) were about
the same.
Single quotes were faster than double quotes, ($arr['index'] vs
$arr["index"], echo 'foo' vs echo "foo", etc)
I would guess $var[index] is as fast as using single quotes; they're
both constant literals.
Of course, things may have changed since PHP5 since then.
Style of writing out strings: In general I prefer always using curly
braces, {}, around variables within strings to help distinguish them.
Wen I concatenate, i put a space on both sides of the dot operator,
starting the new line with the dot op if its a long line. Plus using
{} avoid the whole E_NOTICE discussion :p
Woo, time for my final.
[Back to original message]
|