我有这个网址:
localhost/moked/insert_event.php?custnumber=1234&event=E15&port=8088&areanumber=17
我想检查用户是否已将其更改为例如:
localhost/moked/insert_event.php?custnumber=1234
我知道如何检查变量是否为空,但我想知道用户是否取出了参数。我怎样才能做到这一点?
对单个变量执行此操作非常简单:
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
}
就像是:
if( (!isset($_GET['event'])) || (!isset($_GET['port'])) || ... ) {
//do stuff
}
检查是否存在所需的变量:
if (!isset($_GET['port'])) {
// do something
}
试试isset($_GET['event'])
,,,isset($_GET['port'])
_isset($_GET['areanumber'])
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();
}
检查变量列表:
$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;