Rest assured your function works perfectly. It is returning $type; it's just that $type is blank.
In your function definition, you default $type to "1". This default happens ONLY IF NO VALUE IS PASSED TO THE FUNCTION.
bgcolor("-1") returns "WHITE" because of the:
if ($type=="-1")
{
$type = "WHITE";
}
but if you invoke the function with
bgcolor($num);
the function returns $type, which is BLANK. This is because you are passing a variable ($num) into the function. Since the function default is $type = "1" the uninitialized variable $num is cast into a string of '' (blank). Since your function does not have any default handling for $type = '', the function just returns $type as is; blank.
What you were probably expecting was "WHITE" to be returned; which would be the case if you invoked the function as:
bgcolor(); // note NO variable passed!
Note that NO VARIABLE is not the same as a BLANK VARIABLE or ZERO.
Ahhhh, the subtleties of syntax and type casting...
HTH
-- Rich Rijnders
-- Irvine, CA US