从另一个文件执行php [英] Executing php from another file

查看:35
本文介绍了从另一个文件执行php的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有 1 个模板文件,包含在主文件中.

i have 1 template-file, which is included into main file.

some.template:

...
<?php echo '!'; ?>
...

在主文件中,我将文件读入 1 个变量并显示它.当然,php 脚本不会在那里运行.我该怎么做,我想要什么?

in main file i read file into 1 variable and display it. Of course, php script doesn't run there. How can i do that, what i want?

谢谢

推荐答案

您可以使用 include 来读取和处理文件.如果您需要对结果进行后处理(我认为这可能是您将模板读入变量的原因)尝试 输出缓冲:

You could use include to read and process the file. If you need to post-process the result (I assume that might be the reason for you to read the template into a variable) try output buffering:

ob_start();
include 'some.template';
$out = ob_get_clean();
echo $out;

如果您需要在让 PHP 处理模板之前修改,您将不得不求助于评估.但出于明显的安全原因和可能不那么明显的性能原因,我建议您非常注意这一点.如果你只需要替换一些变量,你应该考虑使用模板引擎或者简单的方法,比如 preg_replace.

If you need to modify the template before having PHP process it, you will have to resort to eval. But I would advise you to be very careful with this for obvious security reasons and maybe-not-so-obvious performance reasons. If you just need some variables to be replaced, you should consider using a template engine instead or simple means like preg_replace.

这篇关于从另一个文件执行php的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆