Variables in Double Quoted Strings

Q

Would I use print "$a dollars" or "{$a} dollars" to print out the amount of dollars in this example?

✍: FYIcenter

A

In this example, "$a dollars" or "{$a} dollars" will print the same result.

But in other cases, you need to use curly braces to protect variable names. For example, for following PHP script will print different results:

<?php
$sales = 7;
echo "$sales000000 dollars\n";
echo "{$sales}000000 dollars\n";
?>

2007-02-27, 5482👍, 0💬