在PHP中运行html代码if

时间:2014-01-16 18:56:29

标签: javascript php html

我有这个PHP代码,我希望它能在PHP if语句中运行一些HTMl代码。

 if ($_GET['report'] == "success")
    {

    RUN HTML CODE HERE

    } else if ($_GET['report'] == "fail") {

    echo 'Error';


      }  

Html代码:

      <li>
       <div class="notification success">
        <p>Success Notification</p>
        <a href="javascript:;" class="close">&times;</a>
        </div> 
      </li>    

3 个答案:

答案 0 :(得分:5)

<?php
if ($_GET['report'] == "success") {
    ?>
    <li>
        <div class="notification success">
            <p>Success Notification</p>
            <a href="javascript:;" class="close">&times;</a>
        </div>
    </li> 
    <?php
} elseif ($_GET['report'] == "fail") {
    echo 'Error';
} 
?>

答案 1 :(得分:3)

您可以关闭您的PHP代码,编写HTML,然后再次打开您的PHP代码。这是一个例子:

<?php
    $some_var = "something";
    if( true ) {
?>
    <div>Some HTML here</div>

<?php } else { ?>

    <div>Some other HTML</div>

<?php } ?>

答案 2 :(得分:2)

我认为这更清洁

if ($_GET['report'] == "success")
{
    echo file_get_contents("templates/my_html_file.html");
}
else if ($_GET['report'] == "fail")
{
    echo 'Error';
}

<强>模板/ my_html_file.html:

<li>
    <div class="notification success">
        <p>Success Notification</p>
        <a href="javascript:;" class="close">&times;</a>
    </div> 
</li>