Is this PHP array syntax valid?

Hi guys,

I have just inherrited the code below:

<?php
$fields  = array();
$fields{"Name"} = "Name";
?>

Is it valid in PHP to use curly brackets (aka braces) instead of square brackets?

It seems to process correctly but I thought it had to be square brackets?

Just a general query.

Cheers,

atw

curlies seem to work equivicantly here; but be careful using them inside strings…


<?php
$var = "I'm A String!";
echo $var[0]; //Outputs "I"
echo $var{0}; //Outputs "I"
echo "This is an $var[0] test of the $var{0} moo cow"; //Outputs "This is an I test of the I'm A String!{0} moo cow"
?>

It is “valid” in the sense of being supported by the language parser, as you can see it obviously works. However, it’s not recommended and certainly not the proper way to be specifying array items.

It does work, but that syntax is deprecated in PHP 5.1 (might be 5.3) and will be removed fully in PHP 6 - see here

i have an idea now, let me try to do it, if it works, i will share here…

Thanks for clearing that up guys.

How weird (both that it exists and that someone would have used it in a script).

The original author would have had to dig deep to find that that was even possible.

Either way, I will avoid it… :slight_smile: