检查 php get 变量是否设置为任何内容?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16999511/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 12:03:11  来源:igfitidea点击:

Check if php get variable is set to anything?

phpgetstrlen

提问by Amy Neville

I need to check if variables are set to something. Up till now I have been using strlen(), but that is really embarrassingas I am pretty sure that is not a very efficient function to be using over an over again.

我需要检查变量是否设置为某些内容。到目前为止,我一直在使用strlen(),但这真的很尴尬,因为我很确定这不是一个非常有效的函数,不能再次使用。

How do I perform this sort of check more efficiently:

我如何更有效地执行此类检查:

if (strlen($_GET['variable']) > 0)
{
    Do Something
}

Note that I don't want it to do anything if $_GET['variable'] = ''

请注意,如果 $_GET['variable'] = ''

Just to clarify what I mean - If I had www.example.com?variable=&somethingelse=1I wouldn't want it to penetrate that if statement

只是为了澄清我的意思 - 如果我有www.example.com?variable=&somethingelse=1我不希望它渗透到 if 语句

回答by hw.

You can try empty.

你可以试试empty

if (!empty($_GET['variable'])) {
  // Do something.
}

On the plus side, it will also check if the variable is set or not, i.e., there is no need to call issetseperately.

从好的方面来说,它还会检查变量是否设置,即不需要isset单独调用。

There is some confusion regarding not calling isset. From the documentation.

关于不调用存在一些混淆isset。从文档中

A variable is considered empty if it does not exist or if its value equals FALSE. empty() does not generate a warning if the variable does not exist.

如果变量不存在或其值等于 FALSE,则该变量被认为是空的。如果变量不存在,empty() 不会生成警告。

and...

和...

That means empty() is essentially the concise equivalent to !isset($var) || $var == false.

这意味着 empty() 本质上是等价于 !isset($var) || $var == 假。

回答by Srikanth Kolli

 if(isset($_GET['variable']) && $_GET['variable']!=""){

}

回答by Pank

if(isset($_GET['variable']) && !empty($_GET['variable']))
{
//Do Something
}

回答by Calin Rusu

If you just want to check if any $_GET is set, without knowing the value just count the $_GET array:

如果您只想检查是否设置了任何 $_GET,而不知道该值,只需计算 $_GET 数组:

<?php
if (count($_GET) == 0):
    // do your stuff
else:
    // do your other stuff
endif;
?>

回答by Steve Taylor

how about just

怎么样

if ($_GET['variable'])
{
     Do Something
}

回答by Fabio

You can use check for isset()but i would rather check also for not blank character with != ''

您可以使用 check forisset()但我宁愿也检查非空白字符 != ''

if (isset($_GET['variable'])) && ($_GET['variable']) != '')