无法在html中运行PHP脚本

时间:2015-04-15 17:17:50

标签: php

当我的特定条件满足时,我一直在尝试在<li>标签之间显示内容。最初我有一个$ result字符串,我试图检查一个条件。但我想我的语法错了。 我的代码:

$Result1 =
'<div id="midrow">
 <ul class="rounded-list">
 <li ><strong>Name:</strong> '.$username.'</li>
     htmlspecialchars('<? if($tips_flag == 1){ ?>');
    <li >Competency 6 - Offer concessions: '.round($per_s6, 2).' percent</li>
     htmlspecialchars('<? } ?>)';
 </ul>
 </div>';

我想这三行不正确,

echo htmlspecialchars('<? if($tips_flag == 1){');
<li >Competency 6 - Offer concessions: '.round($per_s6, 2).' percent</li>
echo htmlspecialchars('<? } ?>)';

帮助Plz!

3 个答案:

答案 0 :(得分:1)

我认为这就是你所追求的(虽然不确定htmlspecialchars)。

$html_additional_content = ($tips_flag == 1) ? '<li>Competency 6 - Offer concessions: ' . round($per_s6, 2) . ' percent</li>' : '';
$Result1 =
'<div id="midrow">
 <ul class="rounded-list">
 <li><strong>Name:</strong> '.$username.'</li>' . $html_additional_content .'
 </ul>
 </div>';

第一行是使用三元运算符的条件。还要确保分配了变量。

答案 1 :(得分:0)

有几种错误的语法错误。我不确定这段代码在做什么,但语法明智,这是错误的:

$Result1 =
'<div id="midrow">
 <ul class="rounded-list">
 <li ><strong>Name:</strong> '.$username.'</li> //you need to put a '; here
    echo htmlspecialchars('<? if($tips_flag == 1){'); //why do you have php 
    //opening there? 

  //here you need to do echo '
    <li >Competency 6 - Offer concessions: '.round($per_s6, 2).' percent</li>
  //you need '; here to end the single tick with a semicolon 
    echo htmlspecialchars('<? } ?>)';

 //you need echo ' here again
 </ul>
 </div>';

答案 2 :(得分:0)

您无法在连接字符串的中间打开和关闭标记(<?php?>)。您也无法将if块放在

$Result1 =
'<div id="midrow">
 <ul class="rounded-list">
 <li ><strong>Name:</strong> ' . $username . '</li>';
if($tips_flag == 1) {
    $Result1 .='<li >Competency 6 - Offer concessions: '.round($per_s6, 2).' percent</li>';
   } 
 $Result1 .= '</ul>
 </div>';