var lifespans, page init question

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
User avatar
keredson
Forum Newbie
Posts: 2
Joined: Mon Apr 28, 2003 12:16 am

var lifespans, page init question

Post by keredson »

let's say i have the following:

Code: Select all

<? 
   $month&#1111;0] = "jan";
   $month&#1111;1] = "feb";
...
   $month&#1111;11] = "dec";
 ?>
this is wasteful, as the PHP engine is going to be creating a static array each time the page is requested. how do i tell the engine to execute this code only ONCE, when the page is initially loaded? and then make the array available for each request of this page made?

thanks,
-- derek
[]InTeR[]
Forum Regular
Posts: 416
Joined: Thu Apr 24, 2003 6:51 am
Location: The Netherlands

Post by []InTeR[] »

What i know is,

that php compiles when you hit the page.
So each code is compiled again, and again, and again.

I know that zend has a 'pre'-compile something.
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

You can use sessions to store information between requests...
http://www.php.net/manual/en/ref.session.php

Mac
User avatar
keredson
Forum Newbie
Posts: 2
Joined: Mon Apr 28, 2003 12:16 am

Post by keredson »

You can use sessions to store information between requests...
http://www.php.net/manual/en/ref.session.php
yep, i considered that, but that only marginally decreases the performance problem. you see, it would still be evaled for each new visitor.

the information is static both across requests for the page AND across visitors for the page. in fact, the contents of the array should never change at all, and it should only ever have to be be created once.

i'm a JSP developer usually - i'm looking for the equiv of the init method, which is run once (and only once) when a JSP or a servlet is first loaded/intrepreted.
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

there is no such method in a php script. Basically the script is executed from the beginning when invoked. There is no persistence between requests (save the cache and a few exceptions).

Code: Select all

<?php
$month = array('jan', 'feb', ..., 'dec');
?>
might speed your example up a bit.
Post Reply