Page 1 of 2

static text generation

Posted: Sat Apr 10, 2004 12:18 am
by damithc
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)?

Posted: Sat Apr 10, 2004 12:21 am
by feyd
sure: fwrite()

Posted: Sat Apr 10, 2004 12:31 am
by damithc
Thanks for the super fast reply. :D

Possibly stupid question (i'm no PHP expert :oops:) : 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?
:?

Posted: Sat Apr 10, 2004 12:35 am
by feyd
yeah. php can be run from a command line interface. You can have your scripts generate all the content you want.. it can even upload it all to the end server when it's done.. it's quite powerful.

Posted: Sat Apr 10, 2004 12:52 am
by damithc
Gr8. Thanks a lot feyd for the replies. Will look in to PHP-CLI :)

Posted: Sat Apr 10, 2004 6:11 am
by damithc
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
-------------------

Posted: Sat Apr 10, 2004 6:20 am
by markl999
Not sure if you'd class this as 'straight forward' but it's just one way of doing it.

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';

?>
Where letter.php would look like:

Code: Select all

Dear Mr <?php echo $name ?>

blah blah blah...
-Thank you

Posted: Sat Apr 10, 2004 7:07 am
by damithc
Thanx very much markl999. It works and it's the most 'straight forward' method I've seen so for (I've seen only one method so far :wink: )
Thanx again. Let's wait and see if there are any other suggestions.

Posted: Sun Apr 11, 2004 10:00 pm
by damithc
Thanx 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 :?:

Posted: Sun Apr 11, 2004 10:38 pm
by markl999
Sure.

Code: Select all

<?php
$A='B';
$B='C';
$BC='ccc';
echo ${$A.$B};
?>
Have a read of variable variables for more info ;)

Posted: Mon Apr 12, 2004 3:26 am
by damithc
Grand. thanks a lot markl999 :D
just out of curiosity, do you have anything you wish PHP could do, but it cannot? :twisted:
i guess not much eh? :wink:

Posted: Mon Apr 12, 2004 7:15 am
by Roja
damithc wrote: just out of curiosity, do you have anything you wish PHP could do, but it cannot? :twisted:
Only one thing on my list..

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.

Posted: Wed May 12, 2004 10:30 am
by dave420
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.

Posted: Thu May 13, 2004 10:36 am
by Roja
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.
You misunderstand my point.

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.
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. :)
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.

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.
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.
I agree. Like I said, the ONLY major issue I've had is with scheduled/recurring events.

Posted: Fri May 14, 2004 3:47 am
by dave420
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.