在PHP中,NULL和将字符串设置为等于2个单引号有什么区别
当我想要空白值时,我曾经设置这样的东西。
$blankVar = '';
几个月后,我认为这看起来更好并且意图更清晰。
$blankVar = null;
这在一段时间内没有出现任何问题,但最近在使用 PDO 准备好的语句时遇到了问题。 将值绑定到 null 会使查询失败,而将其绑定到 '' 则不会。 我需要将它绑定到 null,这样如果满足条件,它就会插入空白数据。
2者有什么区别? 我仍然认为等于 null (或至少是一个常量)看起来更好,所以我应该这样做吗?
define('EMPTY', '');
I used to set things like this when I wanted blank values.
$blankVar = '';
Then after some months, I decided this looked better and had a clearer intent.
$blankVar = null;
This worked without hiccup for a while, but recently with a PDO prepared statements I ran into a problem. Binding a value to null made the query fail, whilst binding it to '' did not. I needed to bind it to null, so that if a condition was met, it would insert blank data.
What are the differences between the 2? I still think equaling null (or at least a constant) looks better, so should I do this?
define('EMPTY', '');
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
Null 只是 PHP 中的另一种数据类型,它只有一个值 (null)。由于 PHP 是一种松散类型语言,因此它如何处理不同的值可能会令人困惑。
"", 0, "0", False, array(), Null
在 PHP 中都被认为是 False。然而,Null 是一种不同的动物。 使用 Null 的主要不兼容性是您无法判断它是否 isset()。
所以 null 很奇怪,因为它不遵循 PHP 中的正常变量规则(至少是一些规则)。 大多数情况下,都没有问题。
当谈到数据库列时,PHP 的 NULL 没有一席之地。您会看到,SQL 是一种基于字符串的语言。 SQL 的 NULL 必须用不带引号的
NULL
表示。因此,如果您想要一个 EMPTY 字段,请将其设置为“”,
但如果您想要一个 NULL 字段,请将其设置为 NULL
BIG DIFFERENCE。
但是,如果您尝试直接插入 PHP NULL,它会向查询添加零个字符(这会导致空白或语法错误,具体取决于您是否引用它)。
Null is just another datatype in PHP, which has only one value (null). Since PHP is a loosly typed language, it can be confusing how it handles different values.
"", 0, "0", False, array(), Null
are all considered False in PHP.Null, however, is a different kind of animal. The main incompatibility with using Null is that you cannot tell if it isset().
So null is odd in the sense that it doesn't follow normal variable rules in PHP (at least some). In most cases, it is fine.
When it comes to database columns, PHP's NULL has no place there. You see, SQL is a string based language. SQL's NULL must be represented by
NULL
with no quotes.So if you want an EMPTY field, set it to ""
But if you want a NULL field, set it to NULL
BIG DIFFERENCE.
But if you try to insert the PHP NULL directly, it will add zero characters to the query, (which leaves you with a blank or syntax error, depending on if you quoted it).
null 是编程语言中的特殊占位符值,字面意思是“无”。 它不是 0,它不是一个空字符串,它什么都没有。 所指向的内存没有任何价值。 另一方面,空字符串仍然是一个字符串对象,只是一个非常短的字符串对象:)
null is a special placeholder value in the programming language that literally means "nothing". It's not 0, it's not an empty string, it's nothing. There is no value in memory being pointed to. An empty string, on the other hand, is still a string object, just a very short one :)
好吧,每种方法看起来有多好是一回事,但主要区别在于,一个是空字符串,另一个是未初始化的变量(null)
Well, it's one thing how nice each approach looks, but the main difference is that one is an empty string and the other is an uninitialized variable (null)
除了 Rex 指出的差异之外,PHP 中还有两种类型的比较,宽松的比较和严格的比较:
http://www.php.net/manual/en/types.comparisons.php
如果以任何方式使用严格比较或 is_null() 等函数,您将得到不同的结果。 然而,通过宽松的比较,PHP 相当宽松。
我不确定,但您也许可以使用 null 方法,然后在遇到问题的上下文中使用变量时进行类型转换(即传递 (string) $blankVar)。 如果有效,则可能意味着您的代码需要进行的更改较少。
Aside from the differences stated by Rex, there's two types of comparisons in PHP, loose and strict:
http://www.php.net/manual/en/types.comparisons.php
If strict comparisons or functions like is_null() are used in any capacity, you'll get different results. With loose comparisons, however, PHP is pretty lenient.
I don't know for sure, but you may be able to use your null approach, then just typecast when you're using the variable in the context where you had issues (i.e. pass (string) $blankVar). If that works, it may mean less changes are necessary to your code.
“null”是未初始化变量所引用的唯一事物。 您可以设置一个变量来引用 null。 还有“未设置”状态,意味着该变量根本不存在。 您可以通过 isset() 函数进行检查。 最常用于检查数组元素是否存在,例如,这是查看 $_GET['op'] 是否已收到查询字符串参数的一种方法。 您还可以通过 unset 取消设置变量(删除数组的元素) () 函数。 还有一个 functionempty() 它将检查变量是否为 NULL、FALSE、0 或空字符串
'null' is the unique thing that uninitialized variables refer to. You can set a variable to refer to null. There is also the 'unset' state meaning the variable doesn't exist at all. You can check that via the isset() function. Most often used to check if an element of array exists and is, for example, a way to see if $_GET['op'] has received a querystring param. You can also make a variable unset (remove an element of an array) via the unset() function. There is also a function empty() which will check if a variable is either NULL, FALSE, 0, or an empty string