|
Posted by Anonymous on 03/11/06 21:37
ColdShine wrote:
> There's no clear example stating this, but:
>
> echo "$array[key]";
>
> Is EXACTLY the same as:
>
> echo "{$array['key']}";
No, Jerry is right, they are not the same.
$array[key] is an array with a constant called key as index,
$array['key'] is an array with the string 'key' as index. You will see
the difference as soon as you assign a value to the constant key. :-)
When using $array[key] PHP will try to find the constant key, but
intelligently will assume you actually meant 'key' when it figures out
that the constant is undefined. Here is the error from the log:
[Sat Mar 11 19:29:40 2006] [error] [client 127.0.0.1] PHP Notice: Use
of undefined constant key - assumed 'key' in C:\\webroot\\test.php on
line 4
The PHP manual says on this topic:
Note: Enabling E_NOTICE during development has some benefits. For
debugging purposes: NOTICE messages will warn you about possible bugs in
your code. For example, use of unassigned values is warned. It is
extremely useful to find typos and to save time for debugging. NOTICE
messages will warn you about bad style. For example, $arr[item] is
better to be written as $arr['item'] since PHP tries to treat "item" as
constant. If it is not a constant, PHP assumes it is a string index for
the array.
However, strangely enough this warning showed up in the log only the
first time I accessed the page! No more warnings on any subsequent
accesses to the page or any other page with that kind of error!
I just checked the config, ignore_repeated_errors and
ignore_repeated_source are set to off. I also confirmed these settings
with phpinfo(). However, PHP reacts as if both are set to on! Did I just
find a bug?
That would explain why you get no warnings! You probably got your first
and only warning a long time ago if you always program like that. ;-)
P.S.: I use PHP 4.4.2 which is the latest released version 4 available.
Can anyone confirm that erroneous logging behaviour of this PHP version?
Navigation:
[Reply to this message]
|