如何从“变量”设置HTML中元素的宽度


How to set the width of an element in HTML from a "variable"

我想设置进度条的宽度,但由于HTML实际上没有变量,我尝试了这个:

<span class="bar" style="width:<?php echo $goal_percent . '%'; ?>"></div>

我知道这是错的,我只是不知道我会怎么做这样的事情?

编辑:这是我使用的更多代码:

                            <?php
                            $goal_percent = $goal / 100;
                            $goal_percent = $goal * 100;
                            if($goal_percent == 0)
                            {
                            $goal_percent = 1;
                            }
                            $goal_percent = 50;
                            ?>
                            <span class="progress progress-success progress-striped active" style="margin-bottom: 9px;">
                        <span class="bar" style="width:<?php echo $goal_percent; ?>%"></span>

我只是为了测试而将目标百分比设置为 50,以防万一数学出了其他问题。仍然不工作。

这段代码将起作用。我只是测试一下:

<html>
    <head><title>Test</title></head>
    <body>
        <?php $goal_percent=50; ?>
        <div class="bar" style="width:<?php echo $goal_percent; ?>%"></div>
    </body>
</html>
只要

$goal_percent是有效的int/float变量。 就像@LawrenceCherone提到的:

<span class="bar" style="width:<?php echo $goal_percent; ?>%"></span>