Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP array, Are array indexes case sensitive?

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.

like image 906
Phill Pafford Avatar asked Oct 02 '09 18:10

Phill Pafford


People also ask

Are arrays case sensitive?

Yes. They are case sensitive. PHP array indexes act as hash tables in your example.

Are array keys case sensitive?

Keys are case sensitive because "key" !== "Key" , because they are different strings.

What is not case sensitive in PHP?

In PHP, class names as well as function/method names are case-insensitive, but it is considered good practice to them functions as they appear in their declaration.

What is case sensitive and case-insensitive in PHP?

Summary. PHP is partially case-sensitive. PHP constructs, function names, class names are case-insensitive, whereas variables are case-sensitive.


1 Answers

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.

like image 61
Dan Herbert Avatar answered Sep 18 '22 11:09

Dan Herbert