php pack(“H*, $s) does not throw exception or return false on error. SOLVED.

I was puzzled by this recently, code that uses pack(“H*”, $s) to compress a token into a few bytes would cause a warning to be displayed by php if the $s token was not a valid hexadecimal string, but still return a bogus result.  try { $p = pack(“H*”, $s); } catch (Exception $e) { } would not catch it, as it is emitted as a warning, not an error.

The warning looks like this:

pack(): Type H: illegal hex digit s in /my/fancy/webapp/problem.php on line 123

After a bunch of unsuccessful googling somebody from effnet reminded me to look up error handling specifics, but  all I  found was how to create error handlers that could catch that sort of thing. But I don’t want to introduce an error handler just yet, instead I am trying to find a simple way to detect failure of the function pack(), i.e. a way to query the error status, so I started experimenting with error_get_last() and came up with the following:

  1. <?php
  2.         function attemptpack($s) {
  3.                 @trigger_error(“”);
  4.                 $p=pack(“H*”, $s);
  5.                 $e = error_get_last();
  6.                 if ($e && !(strpos($e[“message”], “pack()”) === FALSE)) {
  7.                         print(“$s pack error detected:” . $e[“message”] . “\n”);
  8.                 }  else {
  9.                         print “$s packed ok\n”;
  10.                 }
  11.         }
  12.         attemptpack(“abcdef”);
  13.         attemptpack(“axbcdef”);
  14.         attemptpack(“abcdef”);
  15. ?>

Note I am paranoid and am verifying that the message contains pack() in case an older message was not cleared by @trigger. It should not be necessary at this point.

Turns out that does not work on php 5.1.6. And the more obvious way to do this is to validate the input anyways. So now my approach is now much more simple:

  1. <?php
  2. function attemptpack($s) {
  3.     if ( !ctype_xdigit($s))  {
  4.         print __FILE__ . “: $s is not a valid hex string.\n”;
  5.     } else {
  6.         $p=pack(“H*”, $s);
  7.     }
  8. }
  9. attemptpack(“abcdef”);
  10. attemptpack(“abxdef”);
  11. ?>

extjs – my favourite widget-rich javascript library de jour After looking at dojo/scriptaculous/yui/jquery and the like, I finally narrowed it down to extjs ( ) as my favourite playground for a widget-rich browserindependent javascript library. Check out their examples to get excited quickly.

Of course there is no light without shadow: The API documentation is ‘somewhat terse’ (read: lacking links to examples) and the examples that exist outside of that are excellent for showing off what it will look like but leave out the essential pieces that would allow you to learn on how to make it useful for your own application.

I was lucky to find Sakis extjs examples website: which does actually implement forms including the submit functionality and so I was able to glean from it how to use the ajax submit/load functionality.

Another interesting find is which allows you to drag and drop forms together that you can then include in your apps.

Give it a try, if you are shopping for a javascript library that has commercial support and a free community (GPL) version, this is the way to go IMHO.

extjs / php4 on standard rhel4 requires installing json

I was playing with the extjs lib on a standard rhel4 apache/php4 setup, and was wondering why the examples that use the jason encoding routines where not working. After sorting out all the misleading information that google came up with about json requiring php5, I finally have the solution for php4, and it is very simple:

1. Download php-json-ext-1.2.1.tar.bz2 from

2. Extract and install: tar xfj php-json-ext-1.2.1.tar.bz2 ; cd php-json-ext-1.2.1 ; ./configure –prefix=/usr ; make install

3. Edit /etc/php.ini and add the line “”, then do a service httpd reload.

voila, it works: test.php can now do json_encode($arrayname), and all the extjs examples that use json to provide data to the tree-container drag and drop examples like /var/www/html/ext-2.0.1/examples/tree/two-trees.html now work.

I just confirmed it also works with the newer ext-2.1. Now all I have to do is figure out how to create proper trees from dmidecode output 😉