There are multiple ways to include the value of a variable in a string.
One way is to reference the variable inside a double-quoted string:
$bar = "bar";
$a = "foo $bar";
This becomes problematic when the string you are creating itself contains doublequotes. You have to escape the interior doublequotes so that PHP knows they're part of the string and not string delimiters:
$bar = "bar";
$a = "foo \"$bar\"";
To my eye, this is less readable, particularly when you have a lot of interior doublequotes that must be escaped. Like your typical HTML output:
print "<input type=\"text\" name=\"foo\" value=\"$bar\" class=\"nameinput\">";
The workaround I prefer is to limit strings with singlequotes. The problem with this method is that variables are not interpolated inside singlequotes:
$bar = "foo";
print 'foo: "$bar"';
will literally output:
foo: "$bar"
So I break the string into parts and use the "." concatenation operator:
$bar = 'foo';
print 'foo: "' . $bar . '"';
this will output what I expect:
foo: "foo"
My own style of programming in PHP is to use singlequotes to limit strings and thus the concatenation operator. The exception to this rule is when my code is generating SQL queries. Since it is common for SQL queries to have interior singlequotes, I limit string literals with singlequotes -- but I still use the "." operator.
Want the best answers?
Ask the best questions!
TANSTAAFL!!