//include.php
<?php
echo 'ABC';
?>
//buzz.php
<?php
$a = include('include.php);
echo $a
?>
- &GT;输出:ABC1。给我一个解决方案(我知道为什么,不用解释);
答案 0 :(得分:3)
输出缓冲可能是您正在寻找的:
//include.php
<?php
echo 'ABC';
?>
//buzz.php
<?php
ob_start()
include('include.php'); // Added the closing quote, it's missing in your example
$a = ob_get_clean();
echo $a; // ABC
?>
答案 1 :(得分:2)
echo
将输出到输出缓冲区,因此如果在include.php中调用echo一次,则无需尝试在buzz.php中再次调用它。
include()
将返回1
,如果不存在则返回0
,因此当运行include.php时,您的代码将输出ABC
,然后当调用include('include.php')时,它将打印1
,因为该文件确实存在。
只打印ABC
按照Mike的建议或更简单的方法:
//include.php
<?php
echo 'ABC';
?>
//buzz.php
<?php
include('include.php);
?>
最后,您可以从文件返回;
//include.php
<?php
return 'ABC';
?>
//buzz.php
<?php
$a = include('include.php');
echo $a;
?>
答案 2 :(得分:1)
你的问题是:
$a = include('include.php);
如果包含成功, include()
将返回布尔值TRUE,因此$ a变为TRUE,当您输出它时会转换为1
。
答案 3 :(得分:0)
只需使用return
//include.php
return 'ABC';
//buzz.php
$data = include('include.php');
echo $data; // ABC