How to get opcodes of PHP?

PhpOpcode

Php Problem Overview


  <?php
      $show_value   = 123;
      echo 'sing_quote'.$show_value;
      echo "double_quote{$show_value}";

  ?>

Its opcode is:

1: <?php
2: $show_value   = 123;
        0  ASSIGN              !0, 123
3: echo 'sing_quote'.$show_value;
        1  CONCAT              'sing_quote', !0 =>RES[~1]     
        2  ECHO                ~1
4: echo "double_quote{$show_value}";
        3  ADD_STRING          'double_quote' =>RES[~2]     
        4  ADD_VAR             ~2, !0 =>RES[~2]     
        5  ECHO                ~2
        6  RETURN              1

Php Solutions


Solution 1 - Php

Check out the Vulcan Logic Disassembler PECL extension - see author's home page for more info.

> The Vulcan Logic Disassembler hooks > into the Zend Engine and dumps all the > opcodes (execution units) of a script. > It was written as as a beginning of an > encoder, but I never got the time for > that. It can be used to see what is > going on in the Zend Engine.

Once installed, you can use it like this:

php -d vld.active=1 -d vld.execute=0 -f yourscript.php

See also this interesting blog post on opcode extraction, and the PHP manual page listing the available opcodes.

Solution 2 - Php

Parsekit has parsekit_compile_string().

sudo pecl install parsekit

var_dump(parsekit_compile_string(<<<PHP
$show_value   = 123;
echo 'sing_quote'.$show_value;
echo "double_quote{$show_value}";
PHP
));

The output is quite verbose, so you'd need to process it to get assembler-like format.

["opcodes"]=>
array(10) {
[0]=>
array(9) {
["address"]=>
int(44682716)
["opcode"]=>
int(101)
["opcode_name"]=>
string(13) "ZEND_EXT_STMT"
["flags"]=>
int(4294967295)
["result"]=>
array(8) {
["type"]=>
int(8)
["type_name"]=>
string(9) "IS_UNUSED"
["var"]=>
int(0)
["opline_num"]=>
string(1) "0"
["op_array"]=>
string(1) "0"
["jmp_addr"]=>
string(1) "0"
["jmp_offset"]=>
string(8) "35419039"
["EA.type"]=>
int(0)
}
["op1"]=>
array(8) {
["type"]=>
int(8)
["type_name"]=>
string(9) "IS_UNUSED"
["var"]=>
int(0)
["opline_num"]=>
string(1) "0"
["op_array"]=>
string(1) "0"
["jmp_addr"]=>
string(1) "0"
["jmp_offset"]=>
string(8) "35419039"
["EA.type"]=>
int(0)
}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMaskView Question on Stackoverflow
Solution 1 - PhpPaul DixonView Answer on Stackoverflow
Solution 2 - PhpKornelView Answer on Stackoverflow