PHP 在包含之前回显来自包含的变量
PHP echo variable from include before include
我的 index.php 看起来像这样:
<!DOCTYPE html>
<html>
<head>
<title><?php echo $title?></title>
</head>
<body>
<p>Hi, this is my homepage. I'll include some text below.</p>
<?php
include "myfile.php";
?>
</body>
</html>
"myfile.php" 例如包括以下内容:
<?php
$title = "Hi, I'm the title tag…";
?>
<p>Here's some content on the site I included!</p>
<p>
得到输出,但 <title>
标签保持空白。
我怎样才能从包含的站点获取 $title
并在 <body>
标签内显示包含的内容?
还有另一种方法:
index.php
<?php
include "myfile.php";
?>
<!DOCTYPE html>
<html>
<head>
<title><?php echo $title?></title>
</head>
<body>
<p><?php $content; ?></p>
</body>
</html>
myfile.php
<?php
$title = "Hi, I'm the title tag…";
$content = "Here's some content on the site I included!";
?>
这是我的解决方案:
index.php
<?php
// Turn on the output buffering so that nothing is printed when we include myfile.php
ob_start();
// The output from this line now go to the buffer
include "myfile.php";
// Get the content in buffer and turn off the output buffering
$body_content = ob_get_contents();
ob_end_clean();
?>
<!DOCTYPE html>
<html>
<head>
<title><?php echo $title?></title>
</head>
<body>
<p>Hi, this is my homepage. I'll include some text below.</p>
<?php
echo $body_content; // Now, we have the output of myfile.php in a variable, what on earth will prevent us from echoing it?
?>
</body>
</html>
myfile.php
不变。
P.S:按照Noman的建议,你可以先在index.php
的开头include 'myfile.php';
,然后在body标签里面echo $content;
,然后改成myfile.php
变成这样的东西:
<?php
$title = "Hi, I'm the title tag"
ob_start();
?>
<p>Here's some content on the site I included!</p>
<?php
$content = ob_get_contents();
ob_end_clean();
?>
我的 index.php 看起来像这样:
<!DOCTYPE html>
<html>
<head>
<title><?php echo $title?></title>
</head>
<body>
<p>Hi, this is my homepage. I'll include some text below.</p>
<?php
include "myfile.php";
?>
</body>
</html>
"myfile.php" 例如包括以下内容:
<?php
$title = "Hi, I'm the title tag…";
?>
<p>Here's some content on the site I included!</p>
<p>
得到输出,但 <title>
标签保持空白。
我怎样才能从包含的站点获取 $title
并在 <body>
标签内显示包含的内容?
还有另一种方法:
index.php
<?php
include "myfile.php";
?>
<!DOCTYPE html>
<html>
<head>
<title><?php echo $title?></title>
</head>
<body>
<p><?php $content; ?></p>
</body>
</html>
myfile.php
<?php
$title = "Hi, I'm the title tag…";
$content = "Here's some content on the site I included!";
?>
这是我的解决方案:
index.php
<?php
// Turn on the output buffering so that nothing is printed when we include myfile.php
ob_start();
// The output from this line now go to the buffer
include "myfile.php";
// Get the content in buffer and turn off the output buffering
$body_content = ob_get_contents();
ob_end_clean();
?>
<!DOCTYPE html>
<html>
<head>
<title><?php echo $title?></title>
</head>
<body>
<p>Hi, this is my homepage. I'll include some text below.</p>
<?php
echo $body_content; // Now, we have the output of myfile.php in a variable, what on earth will prevent us from echoing it?
?>
</body>
</html>
myfile.php
不变。
P.S:按照Noman的建议,你可以先在index.php
的开头include 'myfile.php';
,然后在body标签里面echo $content;
,然后改成myfile.php
变成这样的东西:
<?php
$title = "Hi, I'm the title tag"
ob_start();
?>
<p>Here's some content on the site I included!</p>
<?php
$content = ob_get_contents();
ob_end_clean();
?>