1
<?php

$value="Hello";

echo '
<table border="1">
<tr>
<td>Row $value, cell 1</td>
<td>Row 1, cell 2</td>
</tr>
</table>
';
?> 

Why i am not getting the Hello instead of $value. How to make it possible.

1
  • 1
    Because you're using single quotes (string literal) Commented Jul 2, 2013 at 18:02

7 Answers 7

4

Basic PHP parsing rules: Strings enclosed with single quotes (') are NOT evaluated by the PHP parser and any variables within are ignored as literal text. Try

echo "
 ...
   $value
 ...
";

instead. Note the use of double-quotes (").

Additionally, please don't use multi-line echoes. They're ugly to maintain. If you have to do multiline output or assignment, consider using a HEREDOC instead.

HEREDOCs remove the need to quote quotes, so your echo becomes:

echo <<<EOL
<table border="1">
   ...
   $values
   ...
</table>
EOL;
Sign up to request clarification or add additional context in comments.

Comments

0

You need to encapsulate it in double quotes so that the processor will evaluate any variables inside.

echo "
<table border='1'>
<tr>
<td>Row $value, cell 1</td>
<td>Row 1, cell 2</td>
</tr>
</table>
";

1 Comment

You will need to escape the double quotes then in the border attribute.
0

You can use variables inside double quotes like that, but not single quotes. Do this instead:

echo '
<table border="1">
  <tr>
    <td>Row '.$value.', cell 1</td>
    <td>Row 1, cell 2</td>
  </tr>
</table>';

Or you can do this and escape the quotes in your string:

echo "
<table border=\"1\">
  <tr>
    <td>Row $value, cell 1</td>
    <td>Row 1, cell 2</td>
  </tr>
</table>";

However, I recommend doing it the first way because it's more efficient and cleaner code.

Comments

0

You can only print Php variables in double quotes, not in single ones.

Or

echo '
<table border="1">
<tr>
<td>Row '.$value.', cell 1</td>
<td>Row 1, cell 2</td>
</tr>
</table>
';
?>

Comments

0

The use of a string over multiple lines has never been a good solution for me. I prefer heredoc:

$value="Hello";

echo <<<EOD
    <table border="1">
    <tr>
        <td>Row {$value}, cell 1</td>
        <td>Row 1, cell 2</td>
    </tr>
    </table>
EOD;

Comments

0
<?php

$value="Hello";

echo '
<table border="1">
<tr>
<td>Row ' . $value . ', cell 1</td>
<td>Row 1, cell 2</td>
</tr>
</table>';
?> 

Comments

-1

Reason is that if you will use single quotes then parser will not look for variables in it. For that either you need to use double quotes or place variable outside single quotes.

<?php

$value="Hello";

echo '
<table border="1">
<tr>
<td>Row '. $value .', cell 1</td>
<td>Row 1, cell 2</td>
</tr>
</table>
';
?> 

OR

<?php

$value="Hello";

echo "
<table border='1'>
<tr>
<td>Row $value, cell 1</td>
<td>Row 1, cell 2</td>
</tr>
</table>
";
?> 

2 Comments

Rather than posting your version of the code, you should include what you think the error was and how to fix it. Just posting a blob of code doesn't help anybody.
Sorry Meagar, i'll fix it.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.