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