Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I echo a variable with single quotes?

Tags:

php

echo

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.

like image 260
RoyRoger78 Avatar asked Jul 12 '11 20:07

RoyRoger78


3 Answers

You must either use:

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

This method appends the variable to the string.

Or you could use:

echo "I love my $variable.";

Notice the double quotes used here!

like image 80
Jaques le Fraque Avatar answered Oct 18 '22 06:10

Jaques le Fraque


No. ' will not parse variables. use

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

or

echo "I love my {$variable}. And I have \" some characters escaped";
like image 29
Nanne Avatar answered Oct 18 '22 04:10

Nanne


If there's a lot of text, have a look at the Heredoc syntax.

$var = <<<EOT
<div style="">Some 'text' {$variable}</div>
EOT;
like image 25
Dogbert Avatar answered Oct 18 '22 06:10

Dogbert