0

我有这个网址:

localhost/moked/insert_event.php?custnumber=1234&event=E15&port=8088&areanumber=17

我想检查用户是否已将其更改为例如:

localhost/moked/insert_event.php?custnumber=1234

我知道如何检查变量是否为空,但我想知道用户是否取出了参数。我怎样才能做到这一点?

4

6 回答 6

9

对单个变量执行此操作非常简单:

if (!isset($_GET['event'])) {
    // event is missing
}

大多数人不知道的是,该isset命令将采用您愿意提供的尽可能多的参数。您可以一次检查所有必需的参数:

if (isset($_GET['custnumber'], $_GET['event'], $_GET['port'], $_GET['areanumber'])) {
    // all parameters are set
} else {
    // some are missing
} 
于 2012-11-19T21:09:15.640 回答
2

就像是:

if( (!isset($_GET['event'])) || (!isset($_GET['port'])) || ... ) {
   //do stuff
}
于 2012-11-19T21:06:33.660 回答
1

检查是否存在所需的变量:

if (!isset($_GET['port'])) {
    // do something
}
于 2012-11-19T21:06:45.680 回答
1

试试isset($_GET['event']),,,isset($_GET['port'])_isset($_GET['areanumber'])

isset()

于 2012-11-19T21:08:25.330 回答
0
try{
    if(!isset($_GET['custnumber'])){
        throw new Exception('Missing "custnumber" parameter');
    }elseif(!isset($_GET['event'])){
        throw new Exception('Missing "event" parameter');
    }elseif(!isset($_GET['port'])){
        throw new Exception('Missing "port" parameter');
    }elseif(!isset($_GET['areanumber'])){
        throw new Exception('Missing "areanumber" parameter');
    }
}catch(Exception $e){
    echo $e->getMessage();
}
于 2012-11-19T21:09:10.177 回答
0

检查变量列表:

$vars=array(
'var1'=>'Var1 Description',
'var2'=>'Var2 Description',
'var3'=>'Var3 Description');
$delim='';
$message='';
foreach($vars as $name=>$description)
{
    if(!isset($_GET[$name]))
    {
        $message.=$delim.$description;
        $delim=', ';
    }
}
if($message!=='')$message='Missing: '.$message;
于 2012-11-19T21:17:08.560 回答