Instead of using
function test($val)...

use: function test(&$val)...
(and note that $val don't change)

I think that using reference will save memory! Tell me if I'm right or wrong.

Thanks