0

I have a question about compiling server side functions in PHP with Ajax. I am trying to understand what happens when multiple asynchronous calls are made to the same server side script.

Lets say that I have the following php script - "msg.php":

<?php

function msg(){
$list1 = "hello world";
return $list1;
}

echo json_encode(msg()); 
?>

and call Ajax JQuery like this:

function get_msg() {
$.get("msg.php", function(data){console.log(JSON.parse(data));}) }

with a button in html:

 input type="button" onclick="console.log(get_msg());" value="Submit" class="btn btn-info" 

Assuming that everything works I am trying to understand the following:

  1. Does the server recompile the "msg" php function each time a user clicks the button? Does it (or can it?) manage this by user? How about by session?
  2. After multiple clicks by different users does the server destroy the "msg" function after each request? Can I save multiple versions of the same function in memory for later use? If so is it possible to reconcile the different versions?
  3. Is there a way to pre-compile a single php function for all Ajax requests?
  4. Is there a "proper" way to handle dynamic function calls on the server side?

I feel like recompiling it on the server side is wasteful but perhaps this is how things are supposed to work.

honeyoak
  • 123
  • 2
  • 5
  • This has nothing to with ajax. You have the same question when you generate a "regular" web page as well. – blockhead Dec 22 '15 at 19:37

2 Answers2

1

PHP interprets code on each request. But there is OPcache:

OPcache improves PHP performance by storing precompiled script bytecode in shared memory, thereby removing the need for PHP to load and parse scripts on each request.

This extension is bundled with PHP 5.5.0 and later, and is available in PECL for PHP versions 5.2, 5.3 and 5.4.

You can find here on SO how to enable OPcache:

Add the following line to your php.ini:

zend_extension=/full/path/to/opcache.so (nix)
zend_extension=C:\path\to\php_opcache.dll (win)

... and much, much more!

Community
  • 1
  • 1
trincot
  • 317,000
  • 35
  • 244
  • 286
0

I think what you're looking for is caching. PHP by default is a scripted language so if you need to reference something repeatedly it will be executed each time since its interpreted not compiled. Caching is a way around that.

There are a few ways I can think of to store this data for later use. One is cacheing in plain text. Write out your result to a text file and then reference that for later use, using the last modified date as a way to check for freshness. I'm not a big fan of this method because I think having extraneous files written to the server is messy. (just my opinion)

http://php.net/manual/en/function.file-put-contents.php

Thats the function you would use to store your result.

Another option is writing to a database for the results and then time stamping it so you can reference that for freshness. From there you could just use PDO or mysqli to retrieve the data. Then you could use a database cache to make the response time more performant.

Another option is just using something like OPCache to store the results of the function in memory on repeated calls. ( I don't have much experience in this though )

Hopefully that puts you in the right direction.

Robert430404
  • 425
  • 5
  • 16
  • OPCache doesn't store the results of the function call. PHP does a just-in-time compilation of each of the php scripts before it runs them. OPCache allows that compilation of the scripts to be cached, so that it only recompiles them when the cache expires. So, the code is still run every time, but the compilation only happens once, speeding up the process for each additional invocation. – Dan Jones May 02 '19 at 13:40