Considering out is the first variable, you don't want to remove the question mark. This may be overkill, but it adds all the needed checks in there.
if(isset($_GET['out'])) {
if(count($_GET) > 1) {
if(substr(current($_SERVER['argv']),0,4) == "out=") {
$newurl = $_SERVER['PHP_SELF']."?".str_replace("out=".$_GET['out']."&","",current($_SERVER['argv']));
}
else {
$newurl = $_SERVER['PHP_SELF']."?".str_replace("&out=".$_GET['out'],"",current($_SERVER['argv']));
}
}
else {
$newurl = $_SERVER['PHP_SELF'];
}
}
else {
$newurl = $_SERVER['PHP_SELF']."?".current($_SERVER['argv']);
}