这是一个OK测试,看看是否设置了变量 [英] Is this an OK test to see if a variable is set

查看:103
本文介绍了这是一个OK测试,看看是否设置了变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

昨天,我发布了一个问题的答案其中包括几个(当时我不知道)非常糟糕的代码示例。从那以后,我一直在研究我对PHP的基本知识,这让我认为这样的代码是可行的。这让我想到一个我似乎无法找到答案的问题:

Yesterday, I posted an answer to a question that included several (unknown to me at the time) very bad code examples. Since then, I've been looking at my fundamental knowledge of PHP that allowed me to think that such code is possible. This brings me to a question that I can't seem to find an answer to:

如果我想检查变量是否有任何设置,是吗? 使用 isset()或其他辅助函数的有效做法?这是一个例如:

If I want to check for whether or not a variable has anything set, is it a valid practice to not use isset() or another helper function? here's a "for instance":

if($not_set){
    //do something
} else {
    //do something else
}

而不是......

if(isset($not_set)){
    //do something
} else {
    //do something else
}

从变量的名称,你可以看到没有设置此变量。因此条件将为false并且 else 部分将运行。到目前为止,我一直在使用这种做法,但在昨天的帖子之后,我现在有一个暗示,这是错误的。

From the name of the variable, you can see that this variable is not set. Therefore the conditional would be false and the else portion would run. Up until now I have been using this practice, but after the posts yesterday, I now have an inkling that this is wrong.

这就是为什么我认为省略上面的 isset()函数是一个好习惯。来自PHP手册:

Here's why I thought that it would be an ok practice to leave out the isset() function above. From PHP manual:


if构造是许多语言中
最重要的功能之一,包括
PHP 。它允许
条件执行代码
片段。 PHP具有if
结构,类似于
C:

The if construct is one of the most important features of many languages, PHP included. It allows for conditional execution of code fragments. PHP features an if structure that is similar to that of C:

if(expr)statement

if (expr) statement

如关于
表达式的部分所述,表达式的
被计算为其布尔值。如果表达式
的计算结果为TRUE,那么PHP将执行
语句,如果计算结果为
FALSE - 它将忽略它。更多
有关将
评估为FALSE的值的信息可以在
'转换为布尔值'部分找到。

As described in the section about expressions, expression is evaluated to its Boolean value. If expression evaluates to TRUE, PHP will execute statement, and if it evaluates to FALSE - it'll ignore it. More information about what values evaluate to FALSE can be found in the 'Converting to boolean' section.

从'转换为布尔部分':

And from the 'Converting to boolean section':


转换为布尔值
时,以下值为考虑
FALSE:

When converting to boolean , the following values are considered FALSE:

...
*特殊类型NULL(包括未设置变量)

... * the special type NULL (including unset variables)

如果这是一个不好的做法,为什么手册会不断说明未包含的变量?如果未设置,它将转换为NULL,因此由条件正确评估。使用 isset()会找到相同的结果,但需要额外的周期才能这样做。

Why would the manual go out of its way to state that unset variables are included if this is a bad practice? If it's unset, it gets converted to NULL and therefore is evaluated properly by the conditional. Using isset() will find the same result, but will take extra cycles to do so.

有人可以请我一直都错了,为什么? (也许它有多糟糕,也许?)

Can someone please enlighten me as to whether I've been wrong this whole time and why? (And just how bad it is, maybe?)

谢谢,所以,你永远不会让人失望。

Thanks, SO, you never disappoint.

编辑:谢谢大家(这很快)。老实说,我认为到目前为止所有的答案都很棒,不知道选择哪个答案......如果你的答案没有被选中,我仍然会投票:o)

Thanks everyone (and that was quick). I honestly think all the answers so far are great and don't know which to pick for the answer... If yours isn't picked, I will still upvote :o)

推荐答案

如果您的变量已设置,您将遇到问题,但评估为FALSE,如下所示:

You will run in to problems if your variable is set, but evaluates to FALSE, like the following:




  • 布尔值 FALSE 本身




  • 整数0(零)




  • 浮点数0.0(零)




  • 空字符串,
    字符串0




  • 一个零元素的数组




  • 一个零成员
    变量的对象(仅限PHP 4)




  • 特殊类型NULL(包括
    未设置变量)



  • 从空
    标签创建的SimpleXML对象

    • the boolean FALSE itself
    • the integer 0 (zero)
    • the float 0.0 (zero)
    • the empty string, and the string "0"
    • an array with zero elements
    • an object with zero member variables (PHP 4 only)
    • the special type NULL (including unset variables)
    • SimpleXML objects created from empty tags
    • 取自PHP手册。

      基本上,使用isset()显示您显式检查变量是否存在且不是NULL,而if语句的结构仅检查变量是否为true。它更清晰,更不容易出错。

      Basically, using isset() is showing that you are explicitly checking if a variable exists and is not NULL, while the structure of your if statement only checks if the variable is true. It is more clear and less error-prone.

      这篇关于这是一个OK测试,看看是否设置了变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆