Buisson
Buisson

Reputation: 539

spacing between square brackets of an array

I have an array where I would like to put spaces between the [] like :

$array[South Africa]=array();

But I can't... why this is not possible?

Upvotes: 0

Views: 743

Answers (1)

Mitch Satchwell
Mitch Satchwell

Reputation: 4830

The correct way of doing this is:

$array['South Africa']  = array();

By not placing quotes around strings, PHP will first check if it is a constant, and if not assume you want to specify the string stated (and generate a warning).

This would work without a space (other than the warning and being bad practise) but with the space PHP thinks the string/constant has ended after 'South' and expects an ]. What you have specified will result in a syntax error:

 unexpected T_STRING, expecting ']'

I personally would avoid using spaces for names/keys anyway but the above explains the problem you are having if you must do this.

Upvotes: 3

Related Questions