包含文件内容php

发布于 2024-12-08 09:07:17 字数 171 浏览 0 评论 0原文

我想将 php 文件中的内容包含到另一个 php 文件中。 (它位于 WordPress 网站中)
Include 或 require 不起作用,因为它只是包含文件,我需要内容本身。
file_get_contents 也不起作用,因为它只显示我的 php 代码而不执行它。

我必须使用什么功能?

I want to include the content from a php file into another php file. (It's in a wordpress site)
Include or require won't work because it's just including the file, I need the content itself.
file_get_contents is also not working because it just displays my php code and does not execute it.

what function do I have to use?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

爱你不解释 2024-12-15 09:07:17

我也不完全理解你的问题,但我认为这可能对你有用:

ob_start();
include('yourfile.php');
$contents = ob_get_clean();

$contents 现在将包含 PHP 脚本的“内容”(输出)。

I don't fully understand your question either, but I think this might work for you:

ob_start();
include('yourfile.php');
$contents = ob_get_clean();

$contents will now contain the 'contents' (the output) of your PHP script.

笑脸一如从前 2024-12-15 09:07:17

包含或要求不起作用,因为它只是包含文件

Pause。什么? 包含 将代码包含在文件中。我确信这就是你想要的。例如,请参阅下面的代码。 test.php 包含 vars.php 并能够回显 vars.php 中的变量

//vars.php
----------
<?php

$color = 'green';
$fruit = 'apple';

?>
----------

//test.php
----------
<?php

echo "A $color $fruit"; // A

include 'vars.php';

echo "A $color $fruit"; // A green apple

?>

Include or require won't work because it's just including the file

Pause. What? Include includes the code in the file for you. I'm sure this is what you want. For instance, see the code below. test.php has vars.php included in it and its able to echo the variables from vars.php

//vars.php
----------
<?php

$color = 'green';
$fruit = 'apple';

?>
----------

//test.php
----------
<?php

echo "A $color $fruit"; // A

include 'vars.php';

echo "A $color $fruit"; // A green apple

?>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文