Instead of writing to a global variable inside the function, why not return the value instead?
function cal ($var1, $var2)
{
if ( $var &bt;= 1 && $var1 <= 4 )
$result = $var2 + $var1;
else
$result = " var1 not in range";
return $result;
}
Note I've simplified the logic somewhat at the same time. Now you can use it like this:
$result = cal($var1, $var2);
print ("the result is $result");
or even better, get rid of $result and just print the return value directly (unless you need it for further computation):
print ("the result is " . cal($var1, $var2) );