在<! - ?php中包含html如果echo“”

时间:2015-11-30 17:42:38

标签: php if-statement include echo

对PHP也很新,我试图让php根据添加到页面的ID动态加载文件。我的想法是为多个页面设置一个文件...我有多个页面加载不同的侧边栏:

<?php

if ($PageType == 'about'){
    echo "
         <img src='http://test.com/images/img.png' alt='' title='' class='img' />
         include('../include-images.html'); 
         <a href='#'><img src='test.com/images/one.jpg' alt='' title='' class='img' /></a>
         <br class='brclear'/>
         ";
}

如何在INCLUDE('../include-images.html');内运行<?php if echo ""的任何帮助?

4 个答案:

答案 0 :(得分:1)

您无法在include内执行echo。只需分解echo

<?php

if ($PageType == 'about'){
    echo "<img src='http://test.com/images/img.png' alt='' title='' class='img' />";
    include('../include-images.html'); 
    echo "<a href='#'><img src='test.com/images/one.jpg' alt='' title='' class='img' /></a>
                            <br class='brclear'/>";
}

答案 1 :(得分:0)

简单地打破回声并包含你需要的HTML,就是这样     

xhrFields: { withCredentials: true }

或者更好,我更喜欢

if ($PageType == 'about'){
    echo "<img src='http://test.com/images/img.png' alt='' title='' class='img' />";
    include('../include-images.html');
    echo "<a href='#'><img src='test.com/images/one.jpg' alt='' title='' class='img' /></a>
                            <br class='brclear'/>
";
}

答案 2 :(得分:0)

当它在字符串文字中时,你不能让PHP运行include,所以这样做。

echo "<img src='http://test.com/images/img.png' alt='' title='' class='img' />";

include('../include-images.html'); 

echo "<a href='#'><img src='test.com/images/one.jpg' alt='' title='' class='img' /></a>
     <br class='brclear'/>
     ";

此外,如果您从自己的网站获取图片,请不要使用src=中的完整域名,只需使用相对路径

echo "<img src='images/img.png' alt='' title='' class='img' />";

include('../include-images.html'); 

echo "<a href='#'><img src='images/one.jpg' alt='' title='' class='img' /></a>
     <br class='brclear'/>
     ";

然后,如果将代码移动到新域名,则无需更改脚本。

答案 3 :(得分:0)

<? if($PageType == 'about') { ?>

<img src='http://test.com/images/img.png' alt='' title='' class='img' />

<? include('../include-images.html'); ?>

<a href='#'><img src='test.com/images/one.jpg' alt='' title='' class='img' /></a>

<br class='brclear'/>

<? } ?>