Skip to content Skip to sidebar Skip to footer

Php Variable In Style (width) Attribute

I have a php echo, and I would like to use a variable for the width attribute to avoid needing an if statement. I tried to use this code: It didn't w

Solution 1:

Does this work:

echo   "<divclass='progress-bar progress-bar-success'role='progressbar'aria-valuenow='25'aria-valuemin='0'aria-valuemax='100'style='width: ".$variable."'>";

Solution 2:

Do not echo the html only echo the variable:

<divclass='progress-bar progress-bar-success'role='progressbar'aria-valuenow='25'aria-valuemin='0'aria-valuemax='100'style='width: <?phpecho$variable?>px'>;

Notice that I also added px after echoing the variable.

Solution 3:

<?php$variable=100'; ?>

echo   "<div class='progress-bar progress-bar-success' role='progressbar' aria-valuenow='25' aria-valuemin='0' aria-valuemax='100' style='width: ( ".$variable.")'>";

Solution 4:

and if you wanna be more fancy you can check if the variable is not empty:

<?phpif($variable != ""):?><!-- this will be the output if variable has some value --><divclass="progress-bar progress-bar-success"role="progressbar"aria-valuenow="25"aria-valuemin="0"aria-valuemax="100"style="width:<?phpecho$variable; ?>px"><?phpelse: ?><!-- some default value --><divclass="progress-bar progress-bar-success"role="progressbar"aria-valuenow="25"aria-valuemin="0"aria-valuemax="100"style="width:100px"><?phpendif;?>

Post a Comment for "Php Variable In Style (width) Attribute"