include file content php

2019-08-31 01:33发布

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?

标签: php file include
2条回答
爱情/是我丢掉的垃圾
2楼-- · 2019-08-31 01:44

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.

查看更多
太酷不给撩
3楼-- · 2019-08-31 02:09

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

?>
查看更多
登录 后发表回答