Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Inserting PHP variable string into inline string doesn't work as it should [duplicate]

Tags:

php

tcpdf

I am a bit rusted in PHP and currently struggling to do something that should be quite simple.

$value = "PUPPY";
$html .= '<td>{$value}</td>';
echo $html; // Doesn't work, prints {$value}.... Should be... PUPPY

I know there's a simple way for doing this but i just forgot and cannot find a definitive method on Google or StackOverflow.

I need to place the html in a variable cause once it is all done, it gets used by tcpdf::writeHtml()

I already tried :

$html .= "<td align='right' nowrap>".$value."</td>";
echo $html; // But this outputs a TD tag with attribute nowrap=""  and this is not valid HTML... tcpdf:writeHtml rejects this.

UPDATE: The bug was in fact caused by a bad usage of the "nowrap" attribute deprecated some time ago.

like image 251
Tuthmosis Avatar asked Nov 29 '22 02:11

Tuthmosis


2 Answers

Single quoted strings are not interpolated in php. Use double quotes instead.

$value = "PUPPY";
$html .= "<td>{$value}</td>";
echo $html;
like image 61
Orangepill Avatar answered May 06 '23 19:05

Orangepill


$value = "PUPPY";
$html .= "<td>{$value}</td>";
echo $html;

Use " instead of '

Take a look at the documentation about Strings on PHP

like image 42
silentw Avatar answered May 06 '23 19:05

silentw