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   "<div class='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:

<div class='progress-bar progress-bar-success' role='progressbar' aria-valuenow='25' aria-valuemin='0' aria-valuemax='100' style='width: <?php echo $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:

<?php if($variable != ""):?>
<!-- this will be the output if variable has some value -->
<div class="progress-bar progress-bar-success" role="progressbar" aria-valuenow="25" aria-valuemin="0" aria-valuemax="100" style="width:<?php echo $variable; ?>px">

<?php else: ?>
<!-- some default value -->
<div class="progress-bar progress-bar-success" role="progressbar" aria-valuenow="25" aria-valuemin="0" aria-valuemax="100" style="width:100px">

<?php endif;?>

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