Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does PHP read required scripts every request?

Tags:

php

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

like image 235
Didar_Uranov Avatar asked Jan 12 '13 09:01

Didar_Uranov


People also ask

How does PHP require work?

PHP Require The require() function copies all of the text from a given file into the file that uses the include function. The require() function produces a fatal error and stops the script's execution if there is a problem loading a file.

How PHP scripts are executed?

The first phase parses PHP source code and generates a binary representation of the PHP code known as Zend opcodes. Opcodes are sets of instructions similar to Java bytecodes. These opcodes are stored in memory. The second phase of Zend engine processing consists in executing the generated opcodes.

Do PHP files need execute permissions?

On php file execution will to preprocessing scripting execution so, No, php-fpm user does NOT need execute permissions on PHP files. It only needs read permission, as PHP files are parsed by PHP preprocessor and not executed directly.

What does require mean in PHP?

The include (or require ) statement takes all the text/code/markup that exists in the specified file and copies it into the file that uses the include statement. Including files is very useful when you want to include the same PHP, HTML, or text on multiple pages of a website.


1 Answers

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.

like image 154
mishmash Avatar answered Sep 22 '22 09:09

mishmash