FAIL! FLAME FLAME FLAME xD
But seriously though, i see alot of places that can be cleaned up like:
if ($2 == $null)
can be replaced with
if (!$2) {
now where was I... oh yeah. FLAMEFLAMEFLAMEFLAMEFLAMEFLAME...