« Posts under php

Compact JS code using PHP

This is a simple function to minimize JS code, it’s very simple and probably fail with more complex cases. But hey, might be useful for someone for something! We would suggest using JSMin::minify() instead.

CakePHP – Simple User Registration

After doing some initial readings on different PHP frameworks, including CakePHP, CodeIgniter, Symphony, Zend. I’m digging deeper into CakePHP with an open eyes for fresh ideas and other developments. To start, CakePHP is slow, quite slow (backed up by Rasmus’s benchmark at froscon). It also has not taken advantage of more object-oriented syntax and methods. That said, there are many neat ideas in CakePHP that we can learn to incorporate into our in-house high performance framework (but with less features and automagic).

Framework Performance

To those who says that “slow frameworks is acceptable or understandable”: good framework scales both in term of performance and functionality. It should be easily extensible from its tiny core. Software that does not use any framework cannot scale to provide more functionality and can offer good performance only at a simple task. Enough rants, let’s get dirty.

First App

This is a simple user registration. Cake does have its way of doing things and you might or might not like it. I’m not a big fan of $validate, $uses, and such. I do hope CakePHP will use more PHP5 and OOP the sooner the better since it’s a good framework.

User Model

User Controller

View – register.ctp

Improve security for apc.php

If you’re running APC with PHP, you have the option to download apc.php to view, monitor the usage/stats. However, the default authentication is very open. Without any credential, anyone can view the running stats and also the cached filenames (a simple search on “APC INFO” will show some site running APC). The login is to view per-directory file caching. So if you want to require login credential on ALL access change this code:

to this:

Linear Regression PHP Class

This PHP 5 class accepts an array of data as input, performs linear regression analysis and returns the trend line. The result is the same as in Excel (Tools/Data Analysis/Regression)

Sample Usage

In this example, we assume it’s a very simple case. For a better forecasting method, there are many, including this multiplicative forecasting method that takes into account seasonality, irregularity and also the growth trend.