Get File Output

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
lenton
Forum Commoner
Posts: 49
Joined: Sun Jun 20, 2010 6:45 am

Get File Output

Post 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?
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

Re: Get File Output

Post by Jonah Bron »

Use include() instead.
User avatar
awebtech
Forum Newbie
Posts: 21
Joined: Sun Nov 07, 2010 4:54 pm
Location: Russian Federation

Re: Get File Output

Post 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");
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

Re: Get File Output

Post 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.
User avatar
awebtech
Forum Newbie
Posts: 21
Joined: Sun Nov 07, 2010 4:54 pm
Location: Russian Federation

Re: Get File Output

Post 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.
Post Reply