| 
	
 | 
 Posted by ColdShine on 03/13/06 03:22 
Anonymous in news:441326E4.66903A5B@nowhere.invalid wrote: 
 
> 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. 
 
Please read my post twice. I'm not talking about using $arr[key], but I'm  
talking 'bout using "$arr[key]". If you can't understand the difference, you  
should not be correcting me. 
 
 
> You will see the difference as soon as you assign a value to the 
> constant key. :-) 
 
No way. The constant called key is ONLY used when referring to $arr[key]  
from OUTSIDE a doublequote'd string, or when using curly syntax. 
 
 
> 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. 
 
Both you and the PHP manual are STILL talking about $arr[item] being used  
OUTSIDE a complex (doublequote'd) string (or again, with curly syntax). 
 
 
> 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. ;-) 
 
If you write $arr[key] you get an E_NOTICE unless there's a constant named  
'key'. 
If you write "$arr[key]" you get an E_NOTICE unless 'key' is a defined index  
in $arr. 
I don't think there's anything complex to understand here. And no, I didn't  
turn on such an idiot-friendly option as ignoring  
warnings/errors/whatsoever. 
 
Here's a quick roundup, if you (or anyone f'wing this thread) still are  
wondering WHAT is meant WHEN: 
 
<?php 
$arr = array('key' => 'foo'); 
define('key', 'another'); 
 
echo $arr[key]; // undefined index ('another') 
echo $arr['key']; // foo 
echo '$arr[key]'; // $arr[key] 
echo "$arr[key]"; // foo 
echo "$arr['key']"; // parse error 
echo "{$arr[key]}"; // undefined index ('another') 
echo "{$arr['key']}"; // foo 
?> 
 
This should rule out any misunderstandings. 
 
Btw, 
<?php 
$arr = array('c' => 'Cheers', 'f' => 'F**k off'); 
define('c', 'f'); 
 
echo "$arr[c] :-)"; 
?> 
--  
ColdShine 
 
"Experience is a hard teacher: she gives the test first, the lesson 
  afterwards." - Vernon Sanders law
 
  
Navigation:
[Reply to this message] 
 |