1

Possible Duplicate:
Difference between single quote and double quote string in php

I need to include some php code within some html file that I generate via PHP.

I have tried the following but this is not working. Can you explain to me why?

<?php
$text='mc';
echo '<text>"$text"</text>'
?>
Community
  • 1
  • 1
Ozan
  • 1,191
  • 2
  • 16
  • 31

5 Answers5

4

Because you should use in this case echo "<text>\"$text\"</text>"

Read more about the different kinds of strings in PHP in this question and in the manual

You can also use String concatenation, like this: echo '<text>' . $text . '</text>'

Which way you prefer is totally up to you.

Community
  • 1
  • 1
Simon Forsberg
  • 13,086
  • 10
  • 64
  • 108
1

It should be:

<?php
$text='mc';
echo '<text>'.$text.'</text>';
?>
ajtrichards
  • 29,723
  • 13
  • 94
  • 101
0
<?php
$text='mc';
echo '<text>'.$text.'</text>'
//or
echo "<text>$text</text>";
?>
Shahrokhian
  • 1,100
  • 13
  • 28
0

You should notice the usage difference between single quote and double quote. The following resource will help you. http://php.net/manual/en/language.types.string.php

SaidbakR
  • 13,303
  • 20
  • 101
  • 195
0

There are a couple of options:

<?php
$text = 'mc';
echo '<text>'.$text.'</text>';
echo '<text>',$text,'</text>';
echo "<text>$text</text>";
echo "<text>{$text}</text>";?>

You can refer to the string manual at php.net for further usage.

Samuel Cook
  • 16,620
  • 7
  • 50
  • 62