2015-04-15 89 views
-1

我一直试图在满足特定条件时在<li>标记之间显示内容。最初我有一个$结果字符串,我试图检查一个条件。但我想我的语法错了。 我的代码:无法在html中运行PHP脚本

$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

您无法在变量赋值中回显。 – chris85

+0

删除回声,但仍然没有成功 – nikhilk

+0

你想用'htmlspecialchars()'做什么?你现在拥有的只是回显纯文本;它不会作为PHP执行,因为您将它作为字符串参数传递。 –

回答

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

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

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