views:

412

answers:

6

I don't know if this is a problem yet but wanted to start thinking about it.

Question:

"Are PHP array indexes case sensitive"?

Example:

$a=array("a"=>"Dog","b"=>"Cat","c"=>"Horse","A"=>"Dog","B"=>"Cat","C"=>"Horse");
print_r($a);

Results:

Array ( [a] => Dog [b] => Cat [c] => Horse [A] => Dog [B] => Cat [C] => Horse )

I've run a couple of examples and this seems to hold true, just wanted to make sure that I'm seeing this correctly.

A: 

I've run a couple of examples and this seems to hold true, just wanted to make sure that I'm seeing this correctly.

Well, if you ran the examples and it's true, there isn't much to do :P

Daniel S
Until, of course, there's that thing you don't know about because you're new to programming.
Alan Storm
True! Sorry for not considering that.
Daniel S
+5  A: 

Yes. They are case sensitive.

PHP array indexes act as hash tables in your example. A capital letter "A" and a lowercase letter "a" have different hash values, therefore they will be different indexes.

Dan Herbert
Thanks for the clarification
Phill Pafford
+1  A: 

Yes, just like variable names (but not function names), hash keys are case-sensitive.

timdev
A: 

Answer:

Yes, they are.

yjerem
A: 

That's easy enough to check on your own.

$dogs = array('Dog' => 'Wuff', 'dog' => 'wuff', 'DOG' => 'WUFF');
var_dump($dogs);
Marko
A: 

Although it's not true of the system with which most people are familiar (Windows), it's a reasonable assumption to make when approaching any new language or environment that it will be case sensitive. PHP is along with virtually every other language and environment in common use. The most notable exceptions that spring to mind (apart from the aforementioned Windows) are SQL and Delphi (Pascal).

Cruachan