[英]PHP variable in div class
我只是想知道是否有可能做這樣的事情:
<?php
if(condition1 > condition2){
$variable = "class1";
}else{
$variable = "class2";
}
?>
<div class=$variable></div>
謝謝。
是的,你可以像使用它一樣
<?php
if(condition1 > condition2){
$variable = "class1";
}else{
$variable = "class2";
}
?>
<div class="<?php echo $variable; ?>" ></div>
你可以使用短的php open + echo標簽:
<?php
if(condition1 > condition2){
$variable = "class1";
}else{
$variable = "class2";
}
?>
<div class=<?=$variable;?>></div>
從php5.4開始永久啟用它們: http ://www.php.net//manual/en/migration54.new-features.php
無論
short_open_tag php.ini
選項如何,<?=
現在始終可用。
確保您的HTML輸出有效且標准。 如果$variable
包含空格或html特殊字符,則可能會遇到問題:
<div class="<?=htmlspecialchars($variable);?>"></div>
當然是的
<div class="<?php echo $variable; ?>"></div>
干杯。
你做得對,只是寫
<div class="<?php echo $variable; ?>" ></div>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.