Page 1 of 1

Get File Output

Posted: Wed Dec 22, 2010 2:07 pm
by lenton
helloworld.php:

Code: Select all

<? echo("Hello World!"); ?>
index.php:

Code: Select all

<?
  $output = file_get_contents("helloworld.php");
  echo($output);
?>
Index.php currently displays "<? echo("Hello World!"); ?>" and I only want it to echo "Hello World" and not the whole php script, is there a function for this?

Re: Get File Output

Posted: Wed Dec 22, 2010 2:46 pm
by Jonah Bron
Use include() instead.

Re: Get File Output

Posted: Wed Dec 22, 2010 3:14 pm
by awebtech
lenton, do you want to grab the output of "helloworld.php" ? If so - specify the full WEB path to this script. For example:

Code: Select all

$output = file_get_contents("http://localhost/helloworld.php");

Re: Get File Output

Posted: Wed Dec 22, 2010 3:51 pm
by Jonah Bron
awebtech wrote:lenton, do you want to grab the output of "helloworld.php" ? If so - specify the full WEB path to this script. For example:

Code: Select all

$output = file_get_contents("http://localhost/helloworld.php");
Bad idea. That requires an unnecessary HTTP request. Capture the output buffer instead.

Code: Select all

ob_start();
include('helloworld.php');
$output = ob_get_flush();
Or if you need to run the other script independendly of the current script, use shell_exec() or backticks (`).

Code: Select all

$output = shell_exec('php helloworld.php');
// or
$output = `php helloworld.php`;
Careful, those are backticks, not single-quotes.

Re: Get File Output

Posted: Wed Dec 22, 2010 4:44 pm
by awebtech
Jonah Bron wrote:Bad idea. That requires an unnecessary HTTP request. Capture the output buffer instead.
It depends on the situation. Output buffering or include usage (which I think is better) are no doubt if the only purpose of "helloworld.php" is to produce some output for the main script and we have direct server access to "helloworld.php" from the main script.