php包含一个变量然后回显该变量

pur*_*111 -1 php echo php-include

我想将一个文件完全包含在变量中.这样我就可以多次调用这个var并保持代码尽可能干净.但是当我回显var时它只返回一个1,当我使用includeon本身时它会输出整个文件.

我想输出包含的文件并运行其中的所有PHP代码.

那么我在这里做错了什么.

如default.php

$jpath_eyecatcher = (JURI::base(). "modules/mod_eyecatcher/tmpl/content/eyecatcher.php");
$jpath_eyecatcher_path = parse_url($jpath_eyecatcher, PHP_URL_PATH);
ob_start();
$eyecatcher = include ($_SERVER['DOCUMENT_ROOT'] . $jpath_eyecatcher_path);
ob_end_clean();


echo $eyecatcher . '<br>';

include ($_SERVER['DOCUMENT_ROOT'] . $jpath_eyecatcher_path);
Run Code Online (Sandbox Code Playgroud)

回声输出是

1

包括输出是

eyecatchertype = 2 
fontawesome
envelope-o
insert_emoticon
custom-icon-class
128
images/clientimages/research (1).jpg
top
test
Run Code Online (Sandbox Code Playgroud)

谢谢您的帮助!

Byt*_*ter 7

使用file_get_contents而不是include()

include()执行文件中给出的php代码,而file_get_contents()则为您提供文件内容.


Mar*_*c B 6

include 不是函数,通常只返回include操作的状态:

docs:

处理返回:包括失败时返回FALSE并引发警告.成功包括,除非被包含文件覆盖,否则返回1.可以在包含的文件中执行return语句,以终止该文件中的处理并返回调用它的脚本.此外,还可以从包含的文件中返回值.

例如

x.php:

<?php
return 42;
Run Code Online (Sandbox Code Playgroud)

y.php

<?php
$y = 'foo';
Run Code Online (Sandbox Code Playgroud)

z.php

<?php
$z = include 'x.php';
echo $z; // outputs 42

$y = include 'y.php';
echo $y; // ouputs 1, for 'true', because the include was successful
         // and the included file did not have a 'return' statement.
Run Code Online (Sandbox Code Playgroud)

另请注意,include只有包含<?php ... ?>代码块才会执行包含的代码.否则包含的任何内容都被视为输出.


Jan*_*lem 5

使用file_get_contentsob_get_clean,像这样:

ob_start();
include ($_SERVER['DOCUMENT_ROOT'] . $jpath_eyecatcher_path);
$eyecatcher = ob_get_clean();
Run Code Online (Sandbox Code Playgroud)