this code can be improved. change php to c++