General Algorithm for PHP