static text generation
Moderator: General Moderators
static text generation
I'm doing a bit of research on 'generative programming languages'. Could anybody please enlighten me on static text generation (as opposed to dynamic html generation) capabilities of PHP, if any?
For example, is there a straight forward way I can use PHP to generate a bunch of text files with slight variations (in a non-web context)?
For example, is there a straight forward way I can use PHP to generate a bunch of text files with slight variations (in a non-web context)?
Thanks for the super fast reply.
Possibly stupid question (i'm no PHP expert
) : imagine i don't have a php enebled web server(Since i don't need any dynamic web pages). Can i still write some sort of PHP script and generate the said text files using that script (a PHP preprocessor kind o thing)?
I guess it's within the capability of the language. But is the necessary infrastructure available?
Does that make sense at all?

Possibly stupid question (i'm no PHP expert
I guess it's within the capability of the language. But is the necessary infrastructure available?
Does that make sense at all?
tried out the PHP-CLI. But still could not find a 'straight forward' way to use PHP in a generative sense. Let me give an example. If i want to generate 4 copies of following text for Name=a,b,c,d and save those in files a.txt b.txt c.txt and d.txt respectively, what would be the direct way of doing this in PHP (let's say using command line)?
-------------------
Dear Mr [Name]
blah blah blah...
-Thank you
-------------------
-------------------
Dear Mr [Name]
blah blah blah...
-Thank you
-------------------
Not sure if you'd class this as 'straight forward' but it's just one way of doing it.
Where letter.php would look like:
Code: Select all
<?php
//array of names we want to generate the .txt files for
$names = array('Smith', 'Jones', 'Anderson');
//loop over them
foreach($names as $name){
//turn on output buffering to generate the output/txt file
ob_start();
//require our txt 'template'
require 'letter.php';
$output = ob_get_contents();
ob_end_clean();
//open the txt file
$fp = fopen($name.'.txt', 'w');
//write the output to the file
fputs($fp, $output);
fclose($fp);
}
echo 'Finished';
?>Code: Select all
Dear Mr <?php echo $name ?>
blah blah blah...
-Thank youThanx for all who helped me in this thread. With your help, i was able to move forward faster in my research. And I must confess I'm impressed by PHP's power 
Now I'm looking into meta-programming aspect of PHP (if any).
For instance, can PHP used for something like below?
$(B($($A)))) should evaluate to 'ccc' if $A='B', $B='C' and $BC='ccc'. That is, recursively evaluated until a literal value is reached.
I know this is invalid syntax. But is there some other way to accomplish the same
Now I'm looking into meta-programming aspect of PHP (if any).
For instance, can PHP used for something like below?
$(B($($A)))) should evaluate to 'ccc' if $A='B', $B='C' and $BC='ccc'. That is, recursively evaluated until a literal value is reached.
I know this is invalid syntax. But is there some other way to accomplish the same
Sure.
Have a read of variable variables for more info 
Code: Select all
<?php
$A='B';
$B='C';
$BC='ccc';
echo ${$A.$B};
?>Only one thing on my list..damithc wrote: just out of curiosity, do you have anything you wish PHP could do, but it cannot?![]()
The abillity to perform an action at a regular interval. Having users required to install cronjobs (on unix) or scheduler entries (on windows) is difficult.
PHP is already a persistent module (mod_php), so it should in theory be able to do such a thing.
mod_php is a module, not an application. That's why it has to be run from another bit of software to do anything. In most cases, that's Apache. PHP isn't its own operating system or daemon, so it can't just run itself at predefined times. It has to be run by something.
Anyway, it's not as if it's hard to schedule a process on any system... cronjobs and scheduled tasks make it trivial.
I've only found a couple of things PHP can't do out of the box, but with the right extensions could very easily do.
Anyway, it's not as if it's hard to schedule a process on any system... cronjobs and scheduled tasks make it trivial.
I've only found a couple of things PHP can't do out of the box, but with the right extensions could very easily do.
You misunderstand my point.dave420 wrote:mod_php is a module, not an application. That's why it has to be run from another bit of software to do anything. In most cases, that's Apache. PHP isn't its own operating system or daemon, so it can't just run itself at predefined times. It has to be run by something.
Apache runs as a daemon, and mod_php runs in Apache, thus mod_php has the POSSIBILITY (if re-architected) to run code at scheduled intervals, or realtime (not dependent on a script).
Yes, mod_php has to be run by Apache - and it is. So when it is, it would be very nice to be able to give it scripts to run realtime.
No, it doesn't. As someone who distributes a fairly popular (top 20 on freshmeat at one point) web-based game, that is my number one compaint with PHP. Making a cross-platform scheduler process isnt just difficult, it requires EXTENSIVE admin interaction. Not to mention, not all shared hosts have shell access, making setting up cron even more complicated. Don't even get me started on how "easy" it is to setup a background, scheduled event thats browser-driven on Microsoft OS's.dave420 wrote: Anyway, it's not as if it's hard to schedule a process on any system... cronjobs and scheduled tasks make it trivial.![]()
It's not easy. If you think it is, I'd love to see code for a cross-platform, no-admin-interaction setup of a scheduled event, because in my experience, you can't do it.
I agree. Like I said, the ONLY major issue I've had is with scheduled/recurring events.dave420 wrote: I've only found a couple of things PHP can't do out of the box, but with the right extensions could very easily do.
PHP is a language, and the PHP software is an interpreter. The interpreter can't just gain consciousness and start running bits of code all on its own - it has to be called by something else. If you want PHP to run at a certain time, something has to run PHP at a certain time. PHP code can't just run itself. Neither can perl, java, C, C++ or anything else. Your gripe seems to be with a lack of cross-platform admin-less scheduling software, not a shortcoming with PHP.