Php difference between 0 and null
Asked 13 years, 11 months ago Show Viewed 163k times I am told that good developers can spot/utilize the difference between
Ben 9,8685 gold badges40 silver badges42 bronze badges asked Sep 26, 2008 at 2:53
stalepretzelstalepretzel 15.1k21 gold badges75 silver badges90 bronze badges 2 It's language specific, but in PHP :
Now, what is tricky, it's that in dynamic languages like PHP, all of them have a value in a boolean context, which (in PHP) is If you test it with So why are they useful ?Well, look at the
And of course, if you deal with states: You want to make a difference between
Robert 1,2461 gold badge17 silver badges37 bronze badges answered Sep 26, 2008 at 11:50
e-satise-satis 559k108 gold badges291 silver badges327 bronze badges 12
there's not much consistency in PHP (though it is improving on latest releases, there's too much backward compatibility). Despite the design wishing some consistency (outlined in the selected answer here), it all get confusing when you consider method returns that use You will often see null being used when they are already using false for something. e.g. filter_input(). They return false if the variable fails the filter, and null if the variable does not exists (does not existing means it also failed the filter?) Methods returning false/null/string/etc interchangeably is a hack when the author care about the type of failure, for example, with Lastly, here's some fun with type juggling. not even including arrays or objects.
answered Apr 18, 2011 at 9:21
9 Below is an example:
Please see this for more reference of type comparisons in PHP. It should give you a clear understanding.
Ariane 3834 silver badges14 bronze badges answered Oct 23, 2015 at 0:44
kriscondevkriscondev 6345 silver badges16 bronze badges 0 In PHP you can use === and !== operators
to check not only if the values are equal but also if their types match. So for example: Now in PHP this quality of values is usually used when returning a value which sometimes can be If you will use the same technique in your functions, anybody familiar with the standard PHP library will understand what is going on and how to check if the returned value is what is wanted or did some error occur while processing. The same actually goes for function params, you can process them differently depending on if they are arrays or strings or what not, and this technique is used throughout PHP heavily too, so everybody will get it quite easily. So I guess that's the power. answered Sep 26, 2008 at 6:20
inkrediblinkredibl 1,8591 gold badge14 silver badges19 bronze badges False, Null, Nothing, 0, Undefined, etc., etc. Each of these has specific meanings that correlate with actual concepts. Sometimes multiple meanings are overloaded into a single keyword or value. In C and C++,
False indicates non-truth. And it used in binary values. It doesn't mean unset, nor does it mean Nothing can indicate that the value is specifically set to be nothing which indicates the same thing as null, but with intent. Undefined in some languages indicates that the value has yet to be set because no code has specified an actual value.
answered Sep 26, 2008 at 3:08
Orion AdrianOrion Adrian 18.7k12 gold badges51 silver badges66 bronze badges 1 I have just wasted 1/2 a day trying to get either a Here's all I was trying to do, before I found that the logic wasn't flowing in the right direction, seeming that there was a blackhole in php coding: Concept take a domain name hosted on a server, and make sure it's not root level, OK several different ways to do this, but I chose different due to other php functions/ constructs I have done. Anyway here was the basis of the cosing:
FINALLY after reading this Topic, I found that this worked!!!
Thanks for this article, I now understand that even though it's Christmas, it may not be Christmas as After wasting a day of debugging some simple code, wished I had known this before, as I would have been able to identify the problem, rather than going all over the place trying to get it to work. It didn't work, as
SherylHohman 15k16 gold badges84 silver badges88 bronze badges answered Dec 22, 2014 at 16:28
From the PHP online documentation:
So, in most cases, it's the same. On the other hand, the
For more information, check the "Comparison Operators" page in the PHP online docs. Hope this helps.
SherylHohman 15k16 gold badges84 silver badges88 bronze badges answered Sep 26, 2008 at 3:56
The differences between these values always come down to detailed language-specific rules. What you learn for PHP isn't necessarily true for Python, or Perl, or C, etc. While it is valuable to learn the rules for the language(s) you're working with, relying on them too much is asking for trouble. The trouble comes when the next programmer needs to maintain your code and you've used some construct that takes advantage of some little detail of Null vs. False (for example). Your code should look correct (and conversely, wrong code should look wrong). answered Sep 26, 2008 at 2:56
Greg HewgillGreg Hewgill 907k177 gold badges1131 silver badges1267 bronze badges Null is used in databases to represent "no record" or "no information". So you might have a bit field that describes "does this user want to be sent e-mails by us", where True means they do, False means they don't want to be sent anything, but Null would mean that you don't know. They can come about through outer joins and suchlike. The logical implications of Null are often different - in some languages NULL is not equal to anything, so if(a == NULL) will always be false. So personally I'd always initialise a boolean to FALSE, and initialising one to NULL would look a bit icky (even in C where the two are both just 0... just a style thing). answered Sep 26, 2008 at 3:11
PeterPeter 7,1362 gold badges33 silver badges46 bronze badges I think bad developers find all different uses of null/0/false in there code. For example, one of the most common mistakes developers make is to return error code in the form of data with a function.
This is an example of a sugar interface. This is exsplained in the book "Debuging the software development proccess" and also in another book "writing correct code". The problem with this, is the implication or assumptions made on the char type. On some compilers the char type can be non-signed. So even though you return a -1 the compiler can return 1 instead. These kind of compiler assumptions in C++ or C are hard to spot. Instead, the best way is not to mix error code with your data. So the following function.
now becomes
This means no matter how young the developer is in your development shop, he or she will never get this wrong. Though this is not talking about redudancy or dependies in code. So in general, swapping bool as the first class type in the language is okay and i think joel spoke about it with his recent postcast. But try not to use mix and match bools with your data in your routines and you should be perfectly fine.
thkala 81.6k23 gold badges152 silver badges198 bronze badges answered Sep 26, 2008 at 3:18
ChadChad 2,8733 gold badges27 silver badges37 bronze badges In PHP it depends on if you are validating types:
Technically null is
answered Sep 26, 2008 at 3:12
Gavin M. RoyGavin M. Roy 4,3614 gold badges35 silver badges29 bronze badges One
interesting fact about
SherylHohman 15k16 gold badges84 silver badges88 bronze badges answered Sep 26, 2008 at 4:01
dirtsidedirtside 7,99610 gold badges41 silver badges53 bronze badges Null is nothing, False is a bit, and 0 is (probably) 32 bits. Not a PHP expert, but in some of the more modern languages those aren't interchangeable. I kind of miss having 0 and false be interchangeable, but with boolean being an actual type you can have methods and objects associated with it so that's just a tradeoff. Null is null though, the absence of anything essentially. answered Sep 26, 2008 at 2:56
CodeRedickCodeRedick 7,2647 gold badges46 silver badges71 bronze badges Well, I can't remember enough from my PHP days to answer the "===" part, but for most C-style languages, NULL should be used in the context of pointer values, false as a boolean, and zero as a numeric value such as an int. '\0' is the customary value for a character context. I usually also prefer to use 0.0 for floats and doubles. So.. the quick answer is: context. answered Sep 26, 2008 at 3:11
jasonmrayjasonmray 2,3002 gold badges19 silver badges14 bronze badges In pretty much all modern languages, null logically refers to pointers (or references) not having a value, or a variable that is not initialized. 0 is the integer value of zero, and false is the boolean value of, well, false. To make things complicated, in C, for example, null, 0, and false are all represented the exact same way. I don't know how it works in PHP. Then, to complicate things more, databases have a concept of null, which means missing or not applicable, and most languages don't have a direct way to map a DBNull to their null. Until recently, for example, there was no distinction between an int being null and being zero, but that was changed with nullable ints. Sorry to make this sound complicated. It's just that this has been a harry sticking point in languages for years, and up until recently, it hasn't had any clear resolution anywhere. People used to just kludge things together or make blank or 0 represent nulls in the database, which doesn't always work too well. answered Sep 26, 2008 at 3:19
Charles GrahamCharles Graham 23.7k14 gold badges43 silver badges56 bronze badges False and 0 are conceptually similar, i.e. they are isomorphic. 0 is the initial value for the algebra of natural numbers, and False is the initial value for the Boolean algebra. In other words, 0 can be defined as the number which, when added to some natural number, yields that same number:
Similarly, False is a value such that a disjunction of it and any other value is that same value:
Null is conceptually something totally different. Depending on the language, there are different semantics for it, but none of them describe an "initial value" as False and 0 are. There is no algebra for Null. It pertains to variables, usually to denote that the variable has no specific value in the current context. In most languages, there are no operations defined on Null, and it's an error to use Null as an operand. In some languages, there is a special value called "bottom" rather than "null", which is a placeholder for the value of a computation that does not terminate. I've written more extensively about the implications of NULL elsewhere. answered Sep 26, 2008 at 4:28
ApocalispApocalisp 34.5k8 gold badges104 silver badges154 bronze badges 1
Somebody can explain to me why 'NULL' is not just a string in a comparison instance?
marc_s 712k171 gold badges1313 silver badges1432 bronze badges answered Feb 1, 2019 at 2:29
vladzurvladzur 4831 gold badge4 silver badges6 bronze badges 1 The issues with falsyness comes from the PHP history. The problem targets the not well defined scalar type.
PHP7 strictness is a step forward, but maybe not enough. https://web-techno.net/typing-with-php-7-what-you-shouldnt-do/ answered Nov 4, 2018 at 12:02
Is 0 and NULL the same in PHP?PHP considers null is equal to zero.
Is 0 true or false in PHP?0 is the integer value of zero, and false is the boolean value of, well, false.
Why is NULL not equal to zero?You can think of it as this, Null just means the value is "undefined". But "0" defines the variable to be an integer, 0 is a value! Null is not declared to be anything. Therefore it is false.
Is 0 considered empty PHP?PHP empty() Function
This function returns false if the variable exists and is not empty, otherwise it returns true. The following values evaluates to empty: 0. 0.0.
|