Use of include is very common in PHP. include will parse a file and display the result. But, how to store the result in a variable for later use? In other words, how to load a php file into a variable?
This example will parse /path/to/include.php and store the result into variable $res
<?php
ob_start(); // turn on output buffering
include('/path/to/include.php');
$res = ob_get_contents(); // get the contents of the output buffer
ob_end_clean(); // clean (erase) the output buffer and turn off output buffering
?>
Remarks
You could use:
<?php
$res = file_get_contents('http://your_server/path/to/include.php');
?>
This method needs ‘allow_url_fopen =
On’ which is a security risk. AVOID this
method and keep allow_url_fopen = Off
. Moreover, in
PHP6, allow_url_fopen will no longer exist.
References
From PHP manual
Entrepreneur | Full-stack developer | Founder of MediSign Ltd. I have over 15 years of professional experience designing and developing web applications. I am also very experienced in managing (web) projects.