Does PHP read required scripts every request?

2020-07-11 06:14发布

Does PHP read required scripts every time while processing new requests ?

Can you explain what disk IO operations are performed by PHP for processing single request

Does something change if PHP is an Apache module or PHP-fpm

标签: php
3条回答
老娘就宠你
2楼-- · 2020-07-11 06:43

Yes.

Imagine it this way:

script_a.php

<?php
$foo = 'bar';
$bar = 'foo';
?>

script_b.php

<?php
require_once 'script_a.php';
echo $foo . ' ' . $bar;
?>

At run-time, script_b.php will actually contain:

<?php
$foo = 'bar';
$bar = 'foo';
echo $foo . ' ' . $bar;
?>

So, it reads the script (or scripts) every time a new request is handled. This is why servers with medium to high load use opcode caches like APC or eAccelerator.

What these do is cache the whole script (with requires/includes processed) in memory so it doesn't have to be processed to bytecode the next request but can just be executed. This can translate to a substantial performance increase because there is no disk I/O and the Zend engine doesn't have to translate the script to bytecode again.

EDIT:

Does something change if PHP is an Apache module or PHP-fpm

No, at least not in the way PHP handles includes/requires.

Hope this helps.

查看更多
做自己的国王
3楼-- · 2020-07-11 07:02

PHP does read the files upon each request. You can use an opcode cacher such as XCache that keep opcode in memory and thus doesn't require PHP to actually read the file (just checks its modified time). FPM and apache module don't influence anything

查看更多
戒情不戒烟
4楼-- · 2020-07-11 07:07

When a PHP script is executed it will be read from the harddrive. Same goes for any scripts included by the originally executed script. There are however certain caching techniques you could employ if you are worried about performance.

See here: http://php.net/manual/en/book.apc.php

查看更多
登录 后发表回答