views:

144

answers:

5

I was just wondering how the PHP is behaving in the background.

Say I have a PHP which creates an array and populates it with names.

$names = Array("Anna", "Jackson" .... "Owen");

Then I have a input field which will send the value on every keypress to the PHP, to check for names containing the value.

Will the array be created on every call? I also sort the array before looping through it, so the output will be alphabetical. Will this take up time in the AJAX call?

If the answer is yes, is there some way to go around that, so the array is ready to be looped through on every call?

+6  A: 

There's no difference between an AJAX request and a "normal" http request. So yes, a new php instance will be created for each request. If the time it takes to parse the script(s) is a problem you can use something like APC.
If those arrays are created at runtime and the time this takes is a problem you might store and share the values between requests in something like memcache

VolkerK
+1  A: 

As far as I know then it will have to create the array each time as the AJAX will make a new server request on each key press on the input input field. Each server request will create the array if you create the script to do so.

A better method would be to use a database to store the names.

sbohan
+1  A: 

Yes it will be created and destroyed every time you run the PHP script.

If this is a problem you could look at persisting this data somewhere (e.g. in a Session or in a Database), but I would ask whether it is really causing you so much of a performance problem that you need to do this?

Tom Haigh
It isn't, it is purely a hypothetical question. I was just playing around with some AJAX calls, and realized that it wasn't really efficient to create the array for each input keypress.
peirix
+2  A: 

No matter what method you use to create the array, if it's in the code, if you pull it out of a database, a text file or any other source, when the web server gets an http request, ( whether it be Ajax or not ) it will start the execution of the PHP script, create its space in memory, and the array will be created. There's only one entry point for a PHP script and it's the first line of it, when an http rquest points to it. (or when another script is included, which is the same)

Petruza
+1  A: 

(it's not a direct answer to your question, but it can help, if you are concerned about performances)

You say this :

Then I have a input field which will send the value on every keypress to the PHP

In this case, it is common pratice to not send the request to the server as soon as a key is pressed : you generally wait a couple of milliseconds (between 100 ms and 150 ms, I'd say), to see if there is not another keypress in that interval :

  • if the user types several keys, he usually types faster than the time you are waiting, so, you only send a request for the last keypress, and not every keypress
    • if the user types 4 letters, you only do 1 request, instead of 4 ; which is great for the health of your server :-)
  • speaking of time for the user : waiting 100 ms plus the time to go to the server, have the script running, and get back from the server is almost the same as without waiting 100 ms first ; so, not bad for the user

As a sidenote : if your liste of data is not too big (20 names is definitly OK ; 100 names would probably be OK ; 1000 might be too much), you could store it directly as a Javascript array, and not do an Ajax request : it's the fastest way (no client-server call), and it won't load your server at all.

Pascal MARTIN
Yeah, I'm using a timeout, set to about 200ms, which is reset every time the user types anything. But it was more a question about the PHP performance, since that's what I'm new to. Nice answer, still (: +1
peirix
OK :-) thanks !
Pascal MARTIN