检查 PHP 中的 null 和缺失查询字符串参数

发布于 2024-10-06 16:43:06 字数 175 浏览 0 评论 0原文

我希望能够区分设置为 null 的现有查询字符串参数和丢失的参数。所以问题的部分是:

  • 如何检查查询字符串中是否存在参数存在 在查询
  • 字符串中传递空值的既定方法是什么? (例如 param=null 或 param=(nothing) )

谢谢

I want to be able to distinguish between existing query string parameters set to null, and missing parameters. So the parts of the question are:

  • How do I check if a parameter exists in the query string
  • What's the established method for passing a null value in a query string? (e.g. param=null or param=(nothing) )

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

夜空下最亮的亮点 2024-10-13 16:43:06

使用 isset()empty()

if (isset($_REQUEST['param']))
{
  // param was set in the query string
   if(empty($_REQUEST['param']))
   {
     // query string had param set to nothing ie ?param=¶m2=something
   }
}

Use isset() and empty()

if (isset($_REQUEST['param']))
{
  // param was set in the query string
   if(empty($_REQUEST['param']))
   {
     // query string had param set to nothing ie ?param=¶m2=something
   }
}
抚笙 2024-10-13 16:43:06

或者使用 array_key_exists

if(array_key_exists("myParam", $_GET)) {

}

我'我从来没有热衷于将空值传递到服务器的“约定” - 例如,我习惯于测试变量是否存在,然后修剪它们并测试是否为空。

Or use array_key_exists:

if(array_key_exists("myParam", $_GET)) {

}

I've never been keen on 'conventions' for passing empty values to the server - I'm used to testing for the presence of variables, and then trimming them and testing for emptiness, for example.

巾帼英雄 2024-10-13 16:43:06

除非在运行时明确设置,否则存储在 $_GET$_POST 中的值只能是字符串或数组。如果您的查询字符串为 query=string,则值为 "string";如果您改用:query=null,则值为 "string"代码>“空”。请注意,它是一个字符串。

如果您发送:query=,则该值将为 "" 或空字符串。请注意 isset。如果值不为 null,则 isset 将为 true;而当值计算结果为 false 时,empty 将为 true。因此,"" 对于两者 issetempty 都为 true。

如果您只想检查查询字符串参数是否设置为 "null" 字符串值,则只需检查 $_GET['query']=='null' (您可能需要在检查之前调整字符的大小写)

Values stored in $_GET and $_POST can only be strings or arrays, unless explicitly set at run-time. If you have a query string of query=string the value is "string" if you instead use: query=null the value will be "null". Note that it is therefor a string.

If you send: query=, the value will be "" or the empty string. Take note of the differences between isset and empty. isset will be true if the value is not null, whereas empty will be true when the value evaluates to false. Therefor "" will be true for both isset and empty.

If you just want to check if a query string parameter was set to the string value of "null", you can simply check $_GET['query']=='null' (you may want to adjust the case of the characters before the check)

提赋 2024-10-13 16:43:06

使用一个 if 语句而不是两个:

if ((isset($_REQUEST['name'])) && (!empty($_REQUEST['name'])))
{
    $name= $_REQUEST['name'];
}

With one if statement instead of two:

if ((isset($_REQUEST['name'])) && (!empty($_REQUEST['name'])))
{
    $name= $_REQUEST['name'];
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文