Fixes and minor changes in php files.

This commit contains mostly fixes or code optimization. All changes are backwards compatible.