Try this for adding new content
$myFile = '../new_folder_name/index.php';
$myContent = 'I love PHP';
$file=fopen($myFile,"a");
fputs($file,$myContent);
fclose($file);
For playing with variables we need to handle an php file as an normal text file here you go
function get_str_btwn($string, $start, $end)//an function to get string between two string in an string
{
$string = " " . $string;
$ini = strpos($string, $start);
if ($ini == 0)
return "";
$ini += strlen($start);
$len = strpos($string, $end, $ini) - $ini;
return substr($string, $ini, $len);
}
$file=file_get_contents($myFile);
if(substr_count($file,"$myContent"))
$file=str_ireplace(get_str_btwn($file,"$myContent =",";"),"I love PHP",$file);//Assuming format of php file is $myContent is initialized like $myContent="something";
else
$file=str_ireplace("<?php","<?php $myContent = 'I love PHP';",$file);
Please note this code is very sensitive and may result in errors in php file generated and is not recommended for regular use unless you are sure format of php file.
file_put_contents()?