在式(宽度)的属性PHP变量(php variable in style (width) attri

2019-10-18 17:28发布

我有一个PHP的回声,我想用一个变量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)'>";

Answer 1:

这是否工作:

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


Answer 2:

不回应的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'>;

请注意,我也呼应了可变后添加像素。



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


Answer 4:

如果你想成为更看中你可以检查变量不为空:

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


Answer 5:

尝试:

$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工程



文章来源: php variable in style (width) attribute