简体   繁体   English

包含一个php文件而不显示其内容

[英]Include a php file without displaying its contents

Is it possible to include a php file without including its contents? 是否可以包含一个不包含其内容的php文件? I just want to access the functions and variables in that file without displaying any content. 我只想访问该文件中的函数和变量,而不显示任何内容。 I tried this 我试过了

<?
    ob_start();
    include('$file');
    ob_end_clean();
?>

But this will hide only contents in php tag. 但这只会隐藏php标记中的内容。 I want to know how to hide others as well. 我也想知道如何隐藏别人。

How to hide? 如何隐藏? Redesign your solution and separate your concerns! 重新设计您的解决方案并分离您的关注点! Do not mix logic with UI and so on. 不要将逻辑与UI等混合使用。

Maybe you should apply the MVC or similar pattern(s). 也许您应该应用MVC或类似模式。

While I totally agree with Peter's answer 虽然我完全同意彼得的回答

I just tried this because I've never tried it before.. 我刚刚尝试了此操作,因为我之前从未尝试过。

File toinclude.php : 文件toinclude.php

<p>Loads of text</p>
<?php
    function my_test()
    {
        echo 'Hello';
    }
?>
<a href="doesntmatterwhere.php">Ooh a link</a>

File includer.php : 文件includer.php

<?php
    ob_start();
    include('toinclude.php');
    ob_end_clean();
    my_test();
?>

And it does work! 它确实有效!

Output: 输出:

Hello

No you can't. 不,你不能。

Include is meant to execute everything inside the file, there are no ways to prevent execution of some part of the file. Include意味着执行文件中的所有内容,没有任何方法可以阻止执行文件的某些部分。 The only way is to edit the included file. 唯一的方法是编辑包含的文件。

如果文件未打印任何输出,则可以尝试eval($fileContent)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM