1

例如,我有一个名为'include.php'or的页面'include.html',其中包含以下代码行:

<h1>Hi {$yourname}, this is a header.</h1>

现在,我想在另一个页面上使用 PHP 包含这个页面,假设'index.php'使用:

<?
 $yourname = 'Bir';
 include 'include.php';
 ?>

当我包含它时,页面显示:

<h1>Hi {$yourname}, this is a header.</h1>

变量$yourname不会被它的 value 替换"Bir"

我该如何解决这个问题?

4

5 回答 5

4

需要include.php写成

<h1>Hi <?php echo $yourname ;?>, this is a header.</h1>
于 2014-03-05T06:20:00.237 回答
1

Another simple way..

test1.php

<?php
$var="<h1>Hi $yourname, this is a header.</h1>"; //Removed {}

test2.php

<?php
$yourname="Jacob";
include_once('test1.php');
echo $var; //"prints" <h1>Hi Jacob, this is a header.</h1>
于 2014-03-05T06:25:02.847 回答
1

Check out http://us3.php.net/str_replace, and replace wherever you see {$yourname} with what ever the value really is.

于 2014-03-05T06:25:09.213 回答
1

如果你想避免,<?php ?>那么你必须使用这样的代码

 <?php echo "<h1>Hi ".$yourname." this is a header.</h1>";
于 2014-03-05T06:25:39.993 回答
0

用这个

<h1>Hi <?= $yourname ?>, this is a header.</h1>
于 2014-03-05T06:21:21.067 回答