18

I want to be able to send a few variables to a file through file_get_contents().

This is firstfile.php:

<?php
$myvar = 'This is a variable';
// need to send $myvar into secondfile.php
$mystr = file_get_contents('secondfile.php');
?>

This is secondfile.php:

The value of myvar is: <?php echo $myvar; ?>

I want the variable $mystr to equal 'The value of myvar is: This is a variable'

Is there any other function that will let you do this in PHP?

Henders
  • 1,154
  • 1
  • 22
  • 25
Richard Westington
  • 181
  • 1
  • 1
  • 3
  • possible duplicate of [What would be the best way to load (with variables) multiple files content into a string?](http://stackoverflow.com/questions/6905364/what-would-be-the-best-way-to-load-with-variables-multiple-files-content-into-a) – Phil Aug 01 '11 at 23:40

5 Answers5

23

There is a big difference between getting the contents of a file and running a script:

  • include — this PHP directive runs the specified file as a script, and the scope is the same as the scope where the include call is made. Therefore, to "pass" variables to it, you simply need to define them before calling include. include can only be used locally (can only include files on the same file system as the PHP server).

  • file_get_contents — When getting a file locally, this simply retrieves the text that is contained in the file. No PHP processing is done, so there is no way to "pass" variables. If you inspect $myvar in your example above, you will see that it contains the exact string "<?php echo $myvar; ?>" — it has not been executed.

    However, PHP has confused some things a little by allowing file_get_contents to pull in the contents of a "remote" file — an internet address. In this case, the concept is the same — PHP just pulls in the raw result of whatever is contained at that address — but PHP, Java, Ruby, or whatever else is running on that remote server may have executed something to produce that result.

    In that case, you can "pass" variables in the URL (referred to as GET request parameters) according to the specifications of the URL (if it is an API, or something similar). There is no way to pass variables of your choosing that have not been specified to be handled in the script that will be processed by that remote server.

    Note: The "remote server" referred to MAY be your own server, though be careful, because that can confuse things even more if you don't really know how it all works (it becomes a second, fully separate request). There is usually not a good reason to do this instead of using include, even though they can accomplish similar results.

Nicole
  • 30,999
  • 11
  • 69
  • 94
18
ob_start();
include 'secondfile.php';
$myvar = ob_get_clean();

Be advised though: to many ob_start's in your code are usually a sign you should define functions which return strings, which you can then choose to echo or not.

Wrikken
  • 64,168
  • 8
  • 83
  • 130
6

I see that this quite an old discussion and my answer may not be quite relevant 7 years later, but just wanted to share how got around the same issue. I just added a dummy text in the secondfile.php (in my case ttt555rrrttt), then I am just replacing that text with the string I want.

$value_to_add = "some value";

$myvar = file_get_contents('secondfile.php');
$myvar = str_replace("ttt555rrrttt", "$value_to_add", $myvar);

That worked for me, and maybe it will work for someone else.

No idea how good it will be from a performance point of view, but having in mind that I am using it to send an email template to a user, the performance shouldn't be a problem.

lStoilov
  • 1,069
  • 2
  • 12
  • 24
4

You may use the sprintf function, for example: firstfile.txt

The value of my var is: %s

secondfile.php

<?php
$f = file_get_contents("firstfile.txt");
$var = "some";
echo sprintf($f, $var);

Result will be

The value of my var is: some

Mozzart
  • 41
  • 1
2

The following code snippet shows an easy way how to send a HTTP post request using the PHP function file_get_contents:

<?php

$url = 'http://url.com/path';
$data = array('param1' => 'value1', 'param2' => 'value2')

// use key 'http' even if you send the request to https://...
$options = array('http' => array(
    'method'  => 'POST',
    'content' => http_build_query($data)
));

$context  = stream_context_create($options);
$result = file_get_contents($url, false, $context);

print_r($result);

OR Passing variable in a URL (php) & file_get_contents()

Community
  • 1
  • 1
Andrey
  • 21
  • 1