SmartHomeBeginner
SmartHomeBeginner

Reputation: 1

Is each php require a separate request?

I am trying to reduce the number of requests on my site (to improve page speed). In one file, I have 10 separate php require statements calling 10 different php files.

My question is, are these 10 require statements considered as 10 separate requests? By replacing the require statements with actual contents from the called php file can I reduce the number of requests?

I would greatly appreciate if someone could please clarify this form. Please note that I am not an experience programmer or web designer. Thanks!

Upvotes: 0

Views: 215

Answers (2)

Joe Watkins
Joe Watkins

Reputation: 17148

As the first comment mentions; requests are only served in response to clients; none of the language constructs of PHP should be described as requests.

As simple as I can make it ...

Execution is a three stage process:

  • Load file from disk
  • Compile into Zend's intermediate form
  • Execute intermediate form

A cache improves performance by usurping those parts of Zend normally responsible for loading files from disk and compiling it to Zend's intermediate representation; such that, if possible, the loading and compilation stage of execution can be bypassed, improving performance.

Without an Opcode Cache

When a request is made such as

GET /index.php HTTP/1.1

The web server servicing the request is invoked in the normal way, which in turn invokes the PHP interpreter. The interpreter then loads index.php from disk, compiles it into Zend's intermediate representation, and begins executing the code.

When a script contains a statement such as

include "my_awesome_code.php";

The (same) interpreter loads my_awesome_code.php from disk, compiles it into Zend's intermediate representation and executes it.

With an Opcode Cache

When a request is made such as

GET /index.php HTTP/1.1

The web server servicing the request is invoked in the normal way, which in turn invokes the PHP interpreter. Rather than loading index.php from the disk, the cache retrieves the code from memory where it is stored in something very close to the form of Zend's intermediate representation. Preparing the cached code (which means copying from shared memory and finalizing it's form) for execution is much faster than loading from disk and compiling to that representation first.

When a script contains a statement such as

include "my_awesome_code.php";

The codes are again retrieved from the cache, bypassing compilation once again.

Should the cache not be able to find index.php or my_awesome_code.php in shared memory, it invokes the parts of Zend it usurped; resulting in the normal loading from disk and compilation of those codes into Zend's intermediate representation. The cache intercepts the resulting intermediate code and stores it in shared memory before allowing it to be executed for the first time.

Upvotes: 3

Adrian Preuss
Adrian Preuss

Reputation: 3113

Like my comment, it's depending how PHP is installed.

If you using PHP as Mod (mod_php), your Webserver create only one PHP process. All requests will be handled here.

But if you have implement PHP as an CGI-Wrapper, each request starts an PHP instance.

To perform your loading time, use Opcode-Caches like eAccelerator, APC or other for PHP Scripts. Other solution is to handle requests with the right way: Cache statical files like Stylesheets, Javascripts, Images,..

Edit

An other solution is: optimize your PHP scripts. A good inspector is New Relic, here you can see, what scripts have very large execution time. I had create an Blog post of New Relic with their features, but it's only in german: http://hovida-design.de/php-die-macht-der-performance/

Upvotes: 0

Related Questions