Difficulty: Medium
Correct Answer: Both "$a dollars" and "{$a} dollars" are valid and output the same result in this simple case, but using braces is a general technique to avoid ambiguity when variables are followed by additional characters.
Explanation:
Introduction / Context:
This question tests knowledge of PHP string interpolation rules. PHP allows variables to be embedded directly inside double quoted strings, which is convenient but can be confusing when variable names sit next to other characters. Understanding when braces are required and when they are optional helps you write readable code and avoid subtle bugs in string output.
Given Data / Assumptions:
Concept / Approach:
In PHP, a variable inside double quotes is parsed and replaced with its value, as long as the parser can clearly see where the variable name ends. When a variable is followed by a character that cannot appear in an identifier, such as a space, the parser can easily separate the variable name from the following text. In those cases, writing "$a dollars" is enough. Braces such as "{$a} dollars" are a more general solution that force the parser to treat everything inside the braces as the variable expression, which becomes important when you want to append letters, array indexes, or object properties immediately after the variable name.
Step-by-Step Solution:
Step 1: Consider the string "$a dollars". After $a there is a space, which cannot appear in a variable name.Step 2: Because of the space, PHP can easily recognise that $a is the variable and dollars is literal text, so the interpolation works and you get something like 5 dollars.Step 3: Now consider "{$a} dollars". Here the braces tell PHP to evaluate the expression inside the braces and then join it with the following text, so the result is again 5 dollars.Step 4: In more complex examples such as "{$a}dollars" or "{$a}USD", braces avoid ambiguity where "$aUSD" might be parsed as a variable called $aUSD instead of $a followed by USD.Step 5: Therefore both forms work in this simple case but braces are a safer habit when additional characters follow directly, which matches option A.
Verification / Alternative check:
You can easily test this in a PHP sandbox by assigning $a = 5 and echoing both forms. You will see that echo "$a dollars"; and echo "{$a} dollars"; produce identical output. Next try echo "$aUSD"; and echo "{$a}USD"; to see how braces resolve ambiguity. This confirms that the conceptual explanation is correct and supports the answer in option A.
Why Other Options Are Wrong:
Option B claims that "$a dollars" is invalid, which is not true since it is a very common pattern. Option C wrongly states that the braced form is invalid. Option D ignores the fact that interpolation with or without braces is supported and claims that concatenation is the only option, which is misleading.
Common Pitfalls:
One pitfall is forgetting braces when appending letters or array keys to a variable name inside a string, which can cause PHP to look for a completely different variable than intended. Another issue is overusing concatenation when simple interpolation would be clearer. A good practice is to use braces whenever you are not sure whether the parser will correctly detect the end of the variable name.
Final Answer:
Both "$a dollars" and "{$a} dollars" are valid and output the same result in this simple case, but using braces is a general technique to avoid ambiguity when variables are followed by additional characters.
Discussion & Comments