Alagu's Personal Weblog
 

Know your Opcodes

Gopal says,

PHP programmers don’t really understand PHP.

They know how to use PHP - but they hardly know how it works,
mainly because it Just Works most of the time.

Very true. A level of abstraction is important for the programmer to concentrate on the logic, than handling cases for memory leaks and socket connections. With that said, it is equally important to know what is being done to the code to give the output.

PHP Code is compiled to Opcodes (like instruction codes) and instruction stack is processed. To see your opcodes, you need pecl/vld. Download it, compile it. For compiling, you might need the php-dev package. In ubuntu, php5-dev is the package name. To install :
$ phpize; ./configure; make; sudo make install
It puts the ‘vld.so’ shared object in your php-lib folder. Check whether vld is installed:
$ php -d extension=vld.so -m
Above command should list all the loaded modules, alongwith vld. Now, try it out :

$ cat test.php
<?
define('MY_BLA',42);
echo MY_BLA;
?>
$ php -d extension=vld.so -d vld.active=1  test.php
Branch analysis from position: 0
Return found
filename:       /tmp/test.php
function name:  (null)
number of ops:  7
compiled vars:  none
line     #  op                           fetch          ext  return  operands
——————————————————————————-
   2     0  SEND_VAL                                                 ‘MY_BLA’
         1  SEND_VAL                                                 42
         2  DO_FCALL                                      2          ‘define’
   3     3  FETCH_CONSTANT                                   ~1      ‘MY_BLA’
         4  ECHO                                                     ~1
   5     5  RETURN                                                   1
         6* ZEND_HANDLE_EXCEPTION

That gives a lot of opcodes! (Remember 8085?) All these opcodes are C macros .

For now, have a look at opcodes for your code and see how you can optimize your it until I come back with the next.

1 Comment »

  1. Ferrel Said,

    June 21, 2008 @ 12:14 pm

    Very true… I mean there are couple of guys in my company who have just managed to get away with understanding the coding for almost 2 years…

RSS feed for comments on this post

Leave a Comment

I work at Yahoo! - the opinions expressed here are my own, and neither Yahoo! nor any other party necessarily agrees with them.