Page 1 of 1

Create new php page using PHP

Posted: Thu Nov 30, 2006 9:08 am
by Tomcat7194
Hello everyone.

I'm looking for a script/function that will allow me to use PHP to create another PHP page based on a template. I want the script to be able to stick some variables into a template, then generate a static PHP page on my server. I know I can use PHP to make a directory, but can I create a template-based file and put it in that directory?

Thanks
Tom

Posted: Thu Nov 30, 2006 9:40 am
by volka
Sure. php doesn't care what it's wrinting to a file, it's just a string.
Simple example

Code: Select all

<?php
$rand = rand(10,99);

$php = <<< eot
<?php
echo $rand;
?>
eot;

file_put_contents('test.create.php', $php);
?>

Posted: Thu Nov 30, 2006 9:57 am
by Burrito
if you're using a version of php prior to 5, you'll need to use fwrite() (there is an example on that page that shows how) to write your file instead of file_put_contents()

Posted: Fri Dec 01, 2006 12:02 am
by feyd
Wouldn't it be easier and/or safer to use a template engine instead of generating php scripts?

Posted: Fri Dec 01, 2006 2:00 am
by Zoxive
feyd wrote:Wouldn't it be easier and/or safer to use a template engine instead of generating php scripts?
Yeah, maybe even righting your own basic one, which then you can easily customize to your likeing, and im sure you will learn alot from it. : p