Hello. Here, for example, there is a file with the contents - for example, "Test" and I connect it through
require and how can I get this text (Test) to a variable?
It looks like nailing a microscope , which would be worth plugging in scripts. In addition, we must remember that
require() will stop execution if there is no file.
Maybe better to look at
Do not complicate the life of a file_get_contents or a buffer; you can make everything easier - drive the contents of the included file into return, when connecting it in another script, return it to a variable - example:
<?php $var = 'PHP'; return $var; ?>
<?php $foo = include 'return.php'; echo $foo; // выведет 'PHP' ?>
I hope that is what was meant
Do you really want this?
ob_start(); include 'file.html'; $html = ob_get_clean();
Without testing, I’m ready to argue that when compared, the aforementioned @xEdelweiss
file_get_contents will be faster.
Upd. And here is the comparison data ! The second answer. From fast to brake:
- file_get_contents ()
- readfile ()
Use the template engine. It can also return the template code to the variable.