Create new php page using PHP

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Tomcat7194
Forum Commoner
Posts: 48
Joined: Mon Jul 31, 2006 1:34 pm

Create new php page using PHP

Post 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
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post 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);
?>
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post 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()
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Wouldn't it be easier and/or safer to use a template engine instead of generating php scripts?
User avatar
Zoxive
Forum Regular
Posts: 974
Joined: Fri Apr 01, 2005 4:37 pm
Location: Bay City, Michigan

Post 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
Post Reply