简体   繁体   English

在 PHP 中,NULL 和将字符串设置为等于 2 个单引号之间有什么区别

[英]In PHP, what is the differences between NULL and setting a string to equal 2 single quotes

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.这工作了一段时间没有打嗝,但最近使用 PDO 准备好的语句我遇到了问题。 Binding a value to null made the query fail, whilst binding it to '' did not.将值绑定到 null 会导致查询失败,而将其绑定到 '' 则不会。 I needed to bind it to null, so that if a condition was met, it would insert blank data.我需要将它绑定到 null,这样如果满足条件,它将插入空白数据。

What are the differences between the 2? 2之间有什么区别? I still think equaling null (or at least a constant) looks better, so should I do this?我仍然认为等于 null (或至少是一个常数)看起来更好,所以我应该这样做吗?

define('EMPTY', '');

Null is just another datatype in PHP, which has only one value (null). Null 只是 PHP 中的另一种数据类型,它只有一个值(null)。 Since PHP is a loosly typed language, it can be confusing how it handles different values.由于 PHP 是一种松散类型的语言,因此可能会混淆它如何处理不同的值。

"", 0, "0", False, array(), Null are all considered False in PHP. "", 0, "0", False, array(), Null在 PHP 中都被认为是 False。

Null, however, is a different kind of animal.然而,Null 是另一种动物。 The main incompatibility with using Null is that you cannot tell if it isset().使用 Null 的主要不兼容之处在于您无法判断它是否为 isset()。

$x = false;
isset($x)  ->  true
echo $x    ->  ""

$y = null;
isset($y)  ->  false
echo $y    ->  ""

//$z is not set
isset($z)  ->  false
echo $z    ->  E_NOTICE

So null is odd in the sense that it doesn't follow normal variable rules in PHP (at least some).所以 null 是奇怪的,因为它不遵循 PHP 中的正常变量规则(至少一些)。 In most cases, it is fine.在大多数情况下,这很好。

When it comes to database columns, PHP's NULL has no place there.说到数据库列,PHP 的 NULL 没有位置。 You see, SQL is a string based language.你看,SQL 是一种基于字符串的语言。 SQL's NULL must be represented by NULL with no quotes. SQL 的NULL必须用不带引号的 NULL 表示。

So if you want an EMPTY field, set it to ""因此,如果您想要一个 EMPTY 字段,请将其设置为“”

INSERT INTO foo SET bar = ""

But if you want a NULL field, set it to NULL但是如果你想要一个 NULL 字段,请将其设置为 NULL

INSERT INTO foo SET bar = 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).但是,如果您尝试直接插入 PHP NULL ,它将在查询中添加零个字符(这会给您留下空白或语法错误,具体取决于您是否引用它)。

null is a special placeholder value in the programming language that literally means "nothing". null是编程语言中的一个特殊占位符值,字面意思是“无”。 It's not 0, it's not an empty string, it's nothing.它不是 0,它不是一个空字符串,它什么都不是。 There is no value in memory being pointed to.指向的 memory 没有任何值。 An empty string, on the other hand, is still a string object, just a very short one:)另一方面,一个空字符串仍然是一个字符串 object,只是一个很短的字符串:)

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)好吧,每种方法看起来有多好是一回事,但主要区别在于一种是空字符串,另一种是未初始化的变量(null)

Aside from the differences stated by Rex, there's two types of comparisons in PHP, loose and strict:除了 Rex 所说的差异之外,PHP 中还有两种比较类型,松散和严格:

http://www.php.net/manual/en/types.comparisons.php 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.如果以任何身份使用严格比较或 is_null() 之类的函数,您将得到不同的结果。 With loose comparisons, however, PHP is pretty lenient.然而,通过松散的比较,PHP 相当宽松。

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 (ie pass (string) $blankVar).我不确定,但是您可以使用 null 方法,然后在遇到问题的上下文中使用变量时进行类型转换(即传递(字符串)$blankVar)。 If that works, it may mean less changes are necessary to your code.如果可行,则可能意味着您的代码需要进行的更改更少。

'null' is the unique thing that uninitialized variables refer to. 'null' 是未初始化变量所指的唯一事物。 You can set a variable to refer to null.您可以设置一个变量来引用 null。 There is also the 'unset' state meaning the variable doesn't exist at all.还有“未设置”的 state 意味着该变量根本不存在。 You can check that via the isset() function .您可以通过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.最常用于检查数组元素是否存在,例如,一种查看 $_GET['op'] 是否收到查询字符串参数的方法。 You can also make a variable unset (remove an element of an array) via the unset() function .您还可以通过unset() function 取消设置变量(删除数组的元素)。 There is also a function empty() which will check if a variable is either NULL, FALSE, 0, or an empty string还有一个function empty()将检查变量是 NULL、FALSE、0 还是空字符串

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM