首页 > 解决方案 > 如何检查GET是否为空?

问题描述

我正在设置一些变量GET

$start = $_GET['start']; 
$end = $_GET['end'];

我从中得到:

start=1-11-2018&end=30-11-2018

然后我在做:

if((!$start) && (!$end)) {
  if (($dateFormat >= $start) && ($dateFormat <= $end)) {
} else {
    echo "no dates";
}

并关闭它

if((!$start) && (!$end)) {
   }
} 

但这没有发生

if((!$start) && (!$end)) {

更新

现在这正在工作,但如果没有 GET,它就不会进入 else

if((!empty($_GET['start'])) && (!empty($_GET['end']))) {
   if (($dateFormat >= $start) && ($dateFormat <= $end)) {
} else {
    echo "No dates";
}

标签: php

解决方案


通过 isset() 检查

如果您打电话:http ://example.com?start=1-11-2018&end=30-11-2018

1. The isset() is checking query string "start"/"end" is having or not. 
2. The empty() is checking query string "start"/"end" is empty/blank or not
if( isset($_GET['start']) && isset($_GET['end']) ){ // check the GET method is set or not

    if((!empty($_GET['start'])) && (!empty($_GET['end']))) {
       if (($dateFormat >= $start) && ($dateFormat <= $end)) {
    }
    else {
        echo "Empty dates";
    }
}
else{
    echo "Start / End date query string is missing...";
}

推荐阅读