views:

2242

answers:

5

I want to get contents of a .php file in a variable on other page.

I have two files, myfile1.php and myfile2.php.

myfile2.php

<?PHP
    $myvar="prashant"; // 
    echo $myvar;
?>

Now I want to get the value echoed by the myfile2.php in an variable in myfile1.php, I have tried the follwing way, but its taking all the contents including php tag () also.

<?PHP
    $root_var .= file_get_contents($_SERVER['DOCUMENT_ROOT']."/myfile2.php", true);
?>

Please tell me how I can get contents returned by one PHP file into a variable defined in another PHP file.

Thanks

+2  A: 

You can use the include directive to do this.

File 2:

<?php
    $myvar="prashant";
?>

File 1:

<?php 

include('myfile2.php');
echo $myvar;

?>
zombat
I know this method already and its working fine, but is there no way other than this?
Prashant
@Prashant And what is your problem with this method? It is indented for doing this.
Török Gábor
Actually I was just looking that is there any "return " type method which can directly give me the value. Anyways I adopted @zombat's answer as the method suggested by @harto may have some performance issues, and I can't compromise with performance. Thanks guyz.
Prashant
+2  A: 

If you only wanted the content echo()'ed by the included page, you could consider using output buffering:

ob_start();
include 'myfile2.php';
$echoed_content = ob_get_clean(); // gets content, discards buffer

See http://php.net/ob_start

harto
ob_start() is new for me. So, @harto can you suggest me which method will do better according to performance, your method or the method @zombat suggested ??
Prashant
Output buffering adds a small performance hit, as there is overhead in initializing and maintaining the buffers.
zombat
@Prashant: I don't have any data available, but I'd guess that the performance impact would be negligible. You could try both methods and see if there's a measurable difference between the two, but I think it would be very small indeed.
harto
+1  A: 

"Actually I was just looking that is there any return type method which can directly give me the value" - You just answered your own question.

See http://sg.php.net/manual/en/function.include.php, Example #5

file1.php:

<? return 'somevalue'; ?>

file2.php:

<?

$file1 = include 'file1.php';
echo $file1; // This outputs 'somevalue'.

?>
lxsg
Ah! I wasn't aware of this.
harto
+2  A: 

You have to differentiate two things:

  • Do you want to capture the output (echo, print,...) of the included file and use the output in a variable (string)?
  • Do you want to return certain values from the included files and use them as a variable in your host script?

Local variables in your included files will always be moved to the current scope of your host script - this should be noted. You can combine all of these features into one:

include.php

$hello = "Hello";
echo "Hello World";
return "World";

host.php

ob_start();
$return = include 'include.php'; // (string)"World"
$output = ob_get_clean(); // (string)"Hello World"
// $hello has been moved to the current scope
echo $hello . ' ' . $return; // echos "Hello World"

The return-feature comes in handy especially when using configuration files.

config.php

return array(
    'host' => 'localhost',
     ....
);

app.php

$config = include 'config.php'; // $config is an array

EDIT

To answer your question about the performance penalty when using the output buffers, I just did some quick testing. 1,000,000 iterations of ob_start() and the corresponding $o = ob_get_clean() take about 7.5 seconds on my Windows machine (arguably not the best environment for PHP). I'd say that the performance impact should be considered quite small...

Stefan Gehrig
A: 

if you want to get all over site use by

http://www.example.com/'; $homepage = file_get_contents($URL); echo $homepage; ?>

vish