sark9012
sark9012

Reputation: 5717

Print string with a php variable in it

For you guys, I imagine this will be easy.

<div class="vote_pct" style="width: $widthpx;">

I want the variable to be $width and for it to have px on the end. If i put a space, it doesnt work. If i put them together, it treats it as one big variable name.

Could I ask someone the correct snytax to achieve this? Thanks

Upvotes: 0

Views: 371

Answers (4)

RobertPitt
RobertPitt

Reputation: 57268

<?php echo sprintf('<div class="%s" style="width: %spx">','vote_pct',$width);?>

Upvotes: 0

Mark Baker
Mark Baker

Reputation: 212402

echo '<div class="vote_pct" style="width: '.$width.'px;">';

or

$width = 5;

echo "<div class=\"vote_pct\" style=\"width: {$width}px;\">";

Upvotes: 1

fabrik
fabrik

Reputation: 14365

If you mix PHP and HTML you can do:

//PHP in HTML
<div class="vote_pct" style="width: <?php echo $width; ?>px;">

HTML in PHP
print '<div class="vote_pct" style="width: ' . $width . 'px;">';

Upvotes: 1

Sjoerd
Sjoerd

Reputation: 75578

  • $bla = '<div class="vote_pct" style="width: '.$width.'px;">';

or

  • $bla = "<div class=\"vote_pct\" style=\"width: ${width}px;\">";

Upvotes: 2

Related Questions