views:

58

answers:

5

i need to load data as array to memory in PHP.but in PHP if i write $array= array("1","2"); in test.php then this $array variable is initialized every time user requests.if we request test.php 100 times by clicking 100 times browser refresh button then this $array variable will be executed 100 times.

but i need to execute the $array variable only one time for first time request and subsequent request of test.php must not execute the $array variable.but only use that memory location.how can i do that in PHP.

but in JAVA SEVRVLET it is easy to execute,just write the $array variable in one time execution of init() method of servlet lifecycle method and subsequent request of that servlet dont execute init() method but service() method but service() method always uses that $array memeory location.

all i want to initilize $array variable once but use that memory loc from subsequent request in PHP.is there any possiblity in PHP?

A: 

PHP has different kind of execution.
In general, it's impossible in PHP and it's OK.

Col. Shrapnel
can i compare the init() method of servlet to PHP??
msaif
or is there any solution of execution of init() method of servlet in PHP??
msaif
@msaif No, there is no init() analogue in the PHP. None. PHP has different kind of execution. It doesn't stay in server's memory. It runs with each request. There is nothing bad with it.
Col. Shrapnel
+1  A: 
Mihai Iorga
1. sessions bound to user. 2. it doesn't solve the problem
Col. Shrapnel
i dont use session. i need to share the same memory location to every user. if i use session then if no of users = 2000 then 2000times session object is loaded into the apache server but i dont grant this.is there any possiblity without session?
msaif
@Col. Shrapnel - I don't the line where he said that he needs for multi users......................................@msaif - use databases, there is no such thing as memory sharing between users like @crrodriguez said 2 posts down.
Mihai Iorga
@msaif what's so special with your server, if you can't grant 2000 object loads while thousands of sites runs well with no such a restriction?
Col. Shrapnel
acutally i can grant but i need a good programming practice like java servlet init() method can execute only once i am looking such thing in PHP
msaif
another users replied that $_GLOBALS is the solution instead of SESSION . is it right?
msaif
@msaif $GLOBALS is even more wrong than SESSION. One who said it have no clue. To create an object with every page load is OK, it IS good programming practice with PHP
Col. Shrapnel
A: 

You can try the following:

<?php
/* test.php */
if (empty($GLOBALS['array'])) {
    $GLOBALS['array'] = array("1", "2");
}
?>
amphetamachine
i have no idea about GLOBALS. is it different that SESSION?if i put data in $_GLOBALS then will the above code be executed only one time for more than one request??
msaif
Sorry, I forgot that that particular special variable ISN'T prefixed with an underscore ("_").
amphetamachine
OK i understand the prefix problem. but will the above code be executed only once ?
msaif
Sorry, I guess I misunderstood the question the first time around.
amphetamachine
+1  A: 

All variables are destroyed at request shutdown, there is no built-in mechanism to do what you want in php.

crrodriguez
+3  A: 

PHP works differently than a Java Servlet container. Every new request basically starts an entirely new instance of the PHP interpreter, therefore you don't have a global adress space across requests (you do have a session per user which gets usually persisted to a file to keep variables across requests for one user).

A thing that might come close to it would be to use memcached with PHP as your "database", but you will have to send a request to the memcached server every time you need your array. That is why I think your array (if it doesn't change) is best kept and initialized in the PHP file.

Daff