dongye1934
2015-04-15 17:17
浏览 30
已采纳

无法在html中运行PHP脚本

I have been trying to display contents between a <li> tag when my particular condition is satisfied. Initially I am having a $result string in which I am trying to check for a condition. But I suppose I am doing wrong with syntax. My code:

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

My these three lines are incorrect I suppose,

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

Help Plz!

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

  • duanliao6789 2015-04-15 17:23
    已采纳

    I think this is what you are after (not sure about the htmlspecialchars though).

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

    That first line is a conditional using the ternary operator. Also make sure your variables there are assigned.

    已采纳该答案
    评论
    解决 无用
    打赏 举报
  • duanjue9296 2015-04-15 17:23

    There are couple of things that are wrong syntax wise. I'm not sure what this code is doing but syntax wise this what's wrong:

    $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>';
    
    评论
    解决 无用
    打赏 举报
  • dongwei3336 2015-04-15 17:24

    You can't open and close tags (<?php and ?>) in the middle of a concatenated string. You also can't put an if block inside either

    $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>';
    
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题