Alex Rivera | Logout

Can I echo a variable with single quotes?

Asked 2011-07-12T20:43:26.293
14

Can I echo in single quotes with a variable?

example

echo 'I love my $variable.';

I need to do this because I have lots of HTML to echo too.

Edit
Report

2 Answers

2

No. Variables (and special characters) only expand in strings delimited with double quotes. If you want to include a variable in a single-quote delimited string, you have to concatenate it instead:

echo 'I love my '.$variable.'.';

You could also escape the double quotes of your HTML if you'd rather use strings delimited by doble quotes:

echo "<a href=\"$url\">$text</a>";

See the PHP manual on strings, especially the part on string parsing, for more information.

answered 2011-07-12T20:46:06.300
2

No. ' will not parse variables. use

echo 'I love my '.$variable.'.';

or

echo "I love my {$variable}. And I have \" some characters escaped";
answered 2011-07-12T20:47:15.240

Your Answer