1

我有一个 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)'>";
4

5 回答 5

4

这是否有效:

echo   "<div class='progress-bar progress-bar-success' role='progressbar' aria-valuenow='25' aria-valuemin='0' aria-valuemax='100' style='width: ".$variable."'>";
于 2013-10-07T16:29:07.903 回答
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”时有效

于 2013-10-07T16:28:26.927 回答
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 。

于 2013-10-07T16:30:49.463 回答
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.")'>";
于 2013-10-07T16:34:18.243 回答
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;?>
于 2013-10-07T16:34:56.790 回答