PHP:检查变量是否存在,但也检查其值是否等于某个值
我有(或没有)一个来自查询字符串的变量 $_GET['myvar']
,我想检查该变量是否存在,以及该值是否对应于我的 if 语句中的某些内容:
什么我正在做并且认为这不是最好的方法:
if(isset($_GET['myvar']) && $_GET['myvar'] == 'something')
:做某事
我的问题是,是否有任何方法可以在不声明变量两次的情况下执行此操作?
这是一个简单的情况,但想象一下必须比较许多 $myvar
变量。
I have (or not) a variable $_GET['myvar']
coming from my query string and I want to check if this variable exists and also if the value corresponds to something inside my if statement:
What I'm doing and think is not the best way to do:
if(isset($_GET['myvar']) && $_GET['myvar'] == 'something')
: do something
My question is, exist any way to do this without declare the variable twice?
That is a simple case but imagine have to compare many of this $myvar
variables.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(14)
从 PHP7 开始,您可以使用 空合并运算符
??
以避免双重引用:Output:
Code demo on 3v4l.org
一般来说,表达式
是等价的请
注意,在代码示例中,需要在
$_GET['myvar'] 周围放置括号? ''
因为==
的优先级高于??
,因此计算结果为:
只要
$_GET['myvar ']
已设置且“真实”(请参阅手册< /a>),否则为 false(因为'' == 'hello'
为 false)。优先级代码3v4l.org 上的演示
As of PHP7 you can use the Null Coalescing Operator
??
to avoid the double reference:Output:
Code demo on 3v4l.org
In general, the expression
is equivalent to
Note that in the code example it is necessary to place parentheses around
$_GET['myvar'] ?? ''
as==
has higher precedence than??
and thuswould evaluate to:
which would be true as long as
$_GET['myvar']
was set and "truthy" (see the manual) and false otherwise (since'' == 'hello'
is false).Precedence code demo on 3v4l.org
遗憾的是,这是唯一的方法。但是有一些方法可以处理更大的数组。例如:
变量 $missing 现在包含所需值的列表,但在 $_GET 数组中缺失。您可以使用 $missing 数组向访问者显示消息。
或者你可以使用类似的东西:
现在每个必需的元素至少有一个默认值。您现在可以使用 if($_GET['myvar'] == 'something') ,而不必担心未设置密钥。
更新
另一种清理代码的方法是使用检查值是否已设置的函数。
Sadly that's the only way to do it. But there are approaches for dealing with larger arrays. For instance something like this:
The variable $missing now contains a list of values that are required, but missing from the $_GET array. You can use the $missing array to display a message to the visitor.
Or you can use something like that:
Now each required element at least has a default value. You can now use if($_GET['myvar'] == 'something') without worrying that the key isn't set.
Update
One other way to clean up the code would be using a function that checks if the value is set.
如果您正在寻找一个单行代码来检查您不确定是否已设置的变量的值,那么这是可行的:
唯一可能的缺点是,如果您正在测试
true
/false
-null
将被解释为等于false
。If you're looking for a one-liner to check the value of a variable you're not sure is set yet, this works:
The only possible downside is that if you're testing for
true
/false
-null
will be interpreted as equal tofalse
.不,如果不进行两次检查,就无法正确执行此操作。 我也讨厌它。
解决这个问题的一种方法是导入所有相关的 GET 变量在一个中心点放入数组或某种类型的对象中(大多数 MVC 框架会自动执行此操作),并设置稍后需要的所有属性。 (而不是通过代码访问请求变量。)
No, there is no way to do this correctly without doing two checks. I hate it, too.
One way to work around it would be to import all relevant GET variables at one central point into an array or object of some sort (Most MVC frameworks do this automatically) and setting all properties that are needed later. (Instead of accessing request variables across the code.)
正如 mellowsoon 所建议的,您可能会考虑这种方法:
您输入默认值并将未收到的参数设置为默认值:)
As mellowsoon suggest, you might consider this approach:
You put the default values and set the not recieved parameters to a default value :)
我从玩弄中发现的一个解决方案是:
A solution that I have found from playing around is to do:
我不是 PHP 专家,但我相信我的解决方案可能有效,因为它对我有用,我将使用我最近使用的代码来完成此操作。
这就是我所做的和逻辑。
I am not a expert in PHP but I believe my solution may work since it worked for me and I will do this with a code I used just recently.
This is what I did and the logic.
感谢 Mellowsoon 和 Pekka,我在这里做了一些研究并提出了这个:
*好的,这个很简单,但工作正常,您可以在此行之后开始在任何地方使用变量
*之后您可以自由使用变量(var1,var2,var3 ...等),
PS:接收JSON对象的函数应该更好(或带有分隔符的简单字符串(用于爆炸/内爆);
...欢迎更好的方法:)
更新:
使用 $_REQUEST 而不是 $_GET,这样你就可以覆盖 $_GET 和 $_POST 变量。
Thanks Mellowsoon and Pekka, I did some research here and come up with this:
*ok this one is simple but works fine, you can start to use the variable everywhere after this line
*after that you are free to use your variables (var1, var2, var3 ... etc),
PS.: function receiving a JSON object should be better (or a simple string with separator for explode/implode);
... Better approaches are welcome :)
UPDATE:
Use $_REQUEST instead of $_GET, this way you cover both $_GET and $_POST variables.
为什么不创建一个函数来执行此操作,将要检查的变量转换为实际变量,例如。
然后你做
_FX('param') == '123'
,只是一个想法why not create a function for doing this, convert the variable your want to check into a real variable, ex.
then you do
_FX('param') == '123'
, just a thought我一直使用自己有用的函数 exst() 来自动声明变量。
例子 -
I use all time own useful function exst() which automatically declare variables.
Example -
这与已接受的答案类似,但使用
in_array
代替。在这种情况下我更喜欢使用empty()
。我还建议使用 PHP 5.4.0+ 中提供的新速记数组声明。这是一个用于一次检查多个值的函数。
This is similar to the accepted answer, but uses
in_array
instead. I prefer to useempty()
in this situation. I also suggest using the new shorthand array declaration which is available in PHP 5.4.0+.Here is a function for checking multiple values at once.
好吧,您只需使用
if($_GET['myvar'] == 'something')
就可以了,因为该条件假定该变量也存在。如果不是,表达式也将导致false
。我认为在上面的条件语句中执行此操作是可以的。确实没有造成任何伤害。
Well, you could get by with just
if($_GET['myvar'] == 'something')
since that condition presumes that the variable also exists. If it doesn't, the expression will also result infalse
.I think it's ok to do this inside conditional statements like above. No harm done really.
没有官方参考,但当我尝试这个时它有效:
No official reference but it worked when I tried this: