样式(宽度)属性中的php变量

时间:2013-10-07 16:26:47

标签: php html css twitter-bootstrap twitter-bootstrap-3

我有一个php echo,我想为width属性使用一个变量来避免需要if语句。我试着使用这段代码:

<?php echo variable; ?>

它不起作用。

这是我的代码:

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

5 个答案:

答案 0 :(得分:4)

这是否有效:

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

答案 1 :(得分:1)

尝试:

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

使用“-string

时可以使用

答案 2 :(得分:1)

不要回显html只回显变量:

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

请注意,我在回显变量后也添加了px。

答案 3 :(得分:1)

<?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.")'>";

答案 4 :(得分:1)

如果你想更加想象,你可以检查变量是否为空:

<?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;?>