I'm glad it helped :)

Global variables are not automatically available within <cms:php> block (much like what happens within a PHP function) and have to explicitly referenced with the 'global' keyword (again exactly how things get done within a PHP function). It is a scoping issue.

Functions, on the other hand, are always available. We used this property to move the bulk of the code into a function and then calling it from within Couch.

Hope the explanation helps.