看看PHP手册吧,写的很清楚.
unset在function里被调用的时候,只清空local的变量,global变量保持unset之前的值.
你把unset($layer)改成$layer=0吧.