Jump to content
Larry Ullman's Book Forums

Associative Array Variable -- Need For Curly Braces In Strings


Recommended Posts

From chapter 2, Introducing Arrays, p. 55:

 

 

...if the array uses strings for the keys, the quotes used to surround the key will muddle the syntax. The following code will cause a parse error:

 

echo "IL is $states['IL']."; // BAD!

 

To fix this, wrap the array name and key in curly braces when an array uses strings for its keys:

 

echo "IL is {$states['IL']}.";

 

Can you elaborate on how the quotes "muddles" the syntax? From what I understand, the curly braces are needed in cases where the last character of the variable name is ambiguous, such as:

<?php
$juice = "apple";

// Invalid. "s" is a valid character for a variable name, but the variable is $juice.
echo "He drank some juice made of $juices.";
// Valid. Explicitly specify the end of the variable name by enclosing it in braces:
echo "He drank some juice made of ${juice}s."
?>

But in the case of an associative array variable, like $states['IL'], what is the potential problem here (what specifically is the need for the braces)?

 

Thank you.

Link to comment
Share on other sites

The specific issue is that it can cause parse errors, although I believe that's been resolved in the most recent versions of PHP (I could be wrong). With your example, that's a fair use, too, although you want to do this, I'm pretty sure:

echo "He drank some juice made of {$juice}s."
Link to comment
Share on other sites

 

>>The specific issue is that it can cause parse errors, although I believe that's been resolved in the most recent versions of PHP (I could be wrong).

 

Thanks for your reply Larry. Yes, I tried the code without the braces and did in fact get a parse error, specifically:

 

 

 

Parse error: syntax error, unexpected '' (T_ENCAPSED_AND_WHITESPACE), expecting identifier (T_STRING) or variable (T_VARIABLE) or number (T_NUM_STRING) in /var/www/html/handle_form.php on line 21
 

I'm using PHP 7.0.13, so it doesn't look like things have changed since the book was published (unless there's some new setting you can change). Of course, my real question is why this code causes a parse error. It's not at all obvious to me what the problem is (and it's not explained in the book).

 

>>With your example, that's a fair use, too, although you want to do this, I'm pretty sure:

echo "He drank some juice made of {$juice}s."

Indeed. The example is taken right from the PHP online manual, and if you take another look at the example code (the "Valid" example), that's exactly what they did.

Link to comment
Share on other sites

I don't have an answer as to why that causes a parse error except to say "because the people that created PHP wrote the parsing script in such a manner that it would". I imagine the issue is that single quotes are otherwise allowed within a double-quoted string, which means PHP breaks it down as $states[ followed by the string 'IL'] and $states[ is obviously an invalid variable name. That's my hunch, anyway.

Link to comment
Share on other sites

I don't have an answer as to why that causes a parse error except to say "because the people that created PHP wrote the parsing script in such a manner that it would". I imagine the issue is that single quotes are otherwise allowed within a double-quoted string, which means PHP breaks it down as $states[ followed by the string 'IL'] and $states[ is obviously an invalid variable name. That's my hunch, anyway.

 

Makes sense to me! Thanks so much.

Link to comment
Share on other sites

 Share

×
×
  • Create New...