简体   繁体   English

PHP样式(宽度)属性的变量

[英]php variable in style (width) attribute

I have a php echo, and I would like to use a variable for the width attribute to avoid needing an if statement. 我有一个php echo,我想为width属性使用一个变量,以避免需要if语句。 I tried to use this code: 我尝试使用此代码:

<?php echo variable; ?>

It didn't work. 没用

Here is my code: 这是我的代码:

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

这项工作:

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

Try: 尝试:

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

Works when you use the "-string 使用“ -string”时有效

Do not echo the html only echo the variable: 不回显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'>;

Notice that I also added px after echoing the variable. 注意,在回显变量后,我还添加了px。

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

and if you wanna be more fancy you can check if the variable is not empty: 如果您想更喜欢,可以检查变量是否不为空:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM