PHP sending variables to file_get_contents()

Viewed 35623

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?

5 Answers

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.

Related