Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP7 cli vs php-fpm

Tags:

php

php-7

I'm running php behind nginx with php-fpm and cron tasks to php binary (/usr/bin/php).

I've found an inconsistency - the same script outputs different results when I run it through php binary and through fpm.

NOTE This only applies to PHP7. On another server I've tested it with 5.6 and the result is identical.

Here's what I've found. The following script:

<?php
class Test {
    public function test(){
        $arr = (object) [
            'children' => []
        ];
        $arr->children[] = 1;
        return $arr;
    }
}

$o = new Test();
$o->test();
print_r( $o->test() );

Saved to test.php. When I run it through browser (php-fpm), will produce:

stdClass Object
(
    [children] => Array
        (
            [0] => 1
        )
)

But when I execute it from CLI, the result is different:

[root@server1 web]# php -f test.php
stdClass Object
(
    [children] => Array
        (
            [0] => 1
            [1] => 1
        )
)

It does not happen without the (object) casting. Also if I'll instantiate $arr with new stdClass() it will not happen.

Seems like the $arr = (object) is being preserved in the memory by php7's engine.

Maybe it's a configuration issue. Anyone ran into it before or can explain?

Thanks.

like image 203
galchen Avatar asked Dec 08 '15 20:12

galchen


People also ask

What is PHP-FPM and CLI?

php-fpm ist running in its own process all the time. It can use apc because it uses continuously the ram over several requests. The memory is only released through the garbage collector or if you kill the fpm process. But the a CLI process lives only for one command and when its finished the memory is released.

Does PHP CLI use FPM?

The standard for running an NGINX webserver for PHP is with PHP-FPM — PHP FastCGI Process Manager(see nginx docs, digitalocean docs).

Is PHP-FPM better?

Conclusion. PHP-FPM is an efficient method on how to minimize the memory consumption and rise the performance for the websites with heavy traffic. It is significantly faster than traditional CGI-based methods in multi-user PHP environments.

How much faster is PHP-FPM?

Results. You can notice PHP-FPM made our test website almost 350% faster when it comes to loading times. Plus, it made the site twice as resource efficient as it was with mod_php. PHP-FPM, one of the newest way to use PHP in conjunction with a web server, is an alternative PHP FastCGI implementation.


1 Answers

This was a bug in PHP 7 which had been reported and is now fixed:

https://bugs.php.net/bug.php?id=71067

like image 148
galchen Avatar answered Oct 19 '22 19:10

galchen