I want to write php codes in a file using php
Try
$cache = "<?php include_once 'a.php';" . $var .ob_get_contents();
file_put_contents('page.php', $cache);
Error
syntax error, unexpected '' (T_ENCAPSED_AND_WHITESPACE), expecting identifier (T_STRING) or variable (T_VARIABLE) or number (T_NUM_STRING)
Thank You
Your syntax is wrong, have a try with this instead to get started:
<?php
$cache = file_get_contents('a.php');
file_put_contents('page.php', $cache);
The documentation of file_put_contents()
clearly states that it requires two arguments (or more). First the path to the file to write to, second the content to write into the file. You provided only a single argument, since you concatenated two things using the .
operator.
However in this case: wouldn't it be easier to just copy the file instead?
Your comment below suggests that you indeed want to concatenate some variables value to what you write into the output file. Just concatenate it then:
<?php
$cache = file_get_contents('a.php');
$cache .= $var;
$cache .= ob_get_contents();
file_put_contents('page.php', $cache);
$cache
is php variable so write inside php code also check syntax in file_put_contents()
<?php
$cache = include_once 'a.php' . $var .ob_get_contents();
file_put_contents('page.php' , $cache);
You might need to split up your <?php
since PHP's behaviour can be a bit weird if it finds another open-tag, even if it is in a string. Try this:
$cache = "<" . "?php include_once 'a.php';" . $var .ob_get_contents();