【问题标题】:Using php variables in included html在包含的 html 中使用 php 变量
【发布时间】:2012-11-03 11:37:12
【问题描述】:

我正在寻找类似于Using PHP variables inside HTML tags? 问题的内容,但略有不同。

就我而言,我想使用这样的代码:

$somevar = 'a test';

include("file.html");

file.html 将包含

<b>hello, this is {$somevar}</b>

问题在于它只打印 hello, this is {$somevar}

如何让 HTML 读取包含文件中的变量?

【问题讨论】:

  • 你需要回显&lt;b&gt;hello, this is &lt;?php echo $somevar; ?&gt;&lt;/b&gt;
  • 任何不在&lt;?php ?&gt; 标签集中,或通过eval()(坏主意,不要这样做)的东西,都不会通过php 解析器运行。
  • @relentless 你有点倒退了,看看 SamuelCook 的回答
  • @Izkata - 是的,你是对的。我没有引起足够的重视。已编辑。

标签: php include var


【解决方案1】:
echo "<b>hello, this is {$somevar}</b>";

<b>hello, this is <?=$somevar?></b>

<b>hello, this is <?php echo $somevar; ?></b>

<b>hello, this is <?php print $somevar; ?></b>

【讨论】:

  • 我不确定。一定有人不喜欢它:/
  • 肯定有人不太喜欢我。
  • 值得注意的是,就像@paquettg 所说,您必须将 file.html 更改为 file.php
  • 您不需要更改文件扩展名。
  • @nickb 在默认 apache 设置下,你可以
【解决方案2】:

您需要在想要访问它的其他程序中包含变量定义程序。 示例:

   Say test.html has $somevar.
   in file.html you do,

   <?php
   include('test.html');
    echo "<b>hello, this is $somevar</b>"; 
   ?>

【讨论】:

    【解决方案3】:
    <?php
    include "stuff.php";
    $somevar = "test";
    ?>
    
    <html>
        <body><p><?php echo($somevar); ?></p></body>
    </html>
    

    【讨论】:

      猜你喜欢
      • 2011-10-05
      • 2013-05-02
      • 1970-01-01
      • 2011-05-23
      • 2013-05-17
      • 2019-04-21
      • 2014-08-27
      • 2017-09-09
      • 1970-01-01
      相关资源
      最近更新 更多