DEV Community

Luca Abbati
Luca Abbati

Posted on

Dump PHP 7.1+ opcodes

If you are working on PHP internals and you need to know which opcodes are generated for a given script, here is the command you can use to dump them.

Assume the file index.php

<?php

echo "hello";
Enter fullscreen mode Exit fullscreen mode

You can dump opcodes running the command

$ php -n -d zend_extension=opcache.so -d opcache.enable=1 -d opcache.enable_cli=1 -d opcache.opt_debug_level=0x10000 index.php
Enter fullscreen mode Exit fullscreen mode
  • -n: [optional] disable all extensions, you will enable only the ones you are interested in;
  • -d zend_extension=opcache.so: load the Zend OPcache extension;
  • -d opcache.enable=1: enable the Zend OPcache extension;
  • -d opcache.enable_cli=1: enable the Zend OPcache extension for the CLI SAPI (required for the command line);
  • -d opcache.opt_debug_level=0x10000: disable opcodes optimization - docs.

The output of this command is

$_main: ; (lines=2, args=0, vars=0, tmps=0)
    ; (before optimizer)
    ; /var/www/html/public/index.php:1-10
L0:     ECHO string("hello")
L1:     RETURN int(1)
Enter fullscreen mode Exit fullscreen mode

Originally, inspired by the excellent Nikita Popov's post about the PHP 7 virtual machine.

Top comments (0)