tags:

views:

36

answers:

4

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

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

or

  • $bla = "<div class=\"vote_pct\" style=\"width: ${width}px;\">";
Sjoerd
Cheers for the answers mate.
Luke
:) or: $bla = "<div class='vote_pct' style='width: $width px;'>";
galambalazs
@galambalazs: you need to escape variables if you inserting them to a string.
fabrik
@fabrik no you don't. Double quotes allow that. Check before you argue... :)
galambalazs
you're right. no escaping but two {}'s needed. Check now.
fabrik
A: 

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;">';
fabrik
Oh dear, I totally forgot to echo it. What am i doing?Awful error to make.Sillyness!Cheers bud
Luke
+1  A: 
echo '<div class="vote_pct" style="width: '.$width.'px;">';

or

$width = 5;

echo "<div class=\"vote_pct\" style=\"width: {$width}px;\">";
Mark Baker
Very appreciated
Luke
A: 
<?php echo sprintf('<div class="%s" style="width: %spx">','vote_pct',$width);?>
RobertPitt
you missed the px part :)
Eineki
changed, im sure he would of found that out anyways :)
RobertPitt