1

When I execute this:

$filename = "test.php";
$content = "<?php $variable = 35 ?>";
file_put_contents($filename,$content);

The file contains this:

<?php  =35?>

How can I get it to include the $variable ?

Also, how can I append this to the top of the file without overwriting existing data?

1 Answer 1

5

Use single quotes to prevent PHP from trying to interpolate $variable.

$content = '<?php $variable = 35 ?>';

Finally, you would append this to the file by using fopen() and passing the 'a' mode, which will append to the bottom of the file

To append to the top, it is a bit more complicated. As far as I know, you will have to read the entire file in and concatenate the old contents to the end of the new contents.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.