ok can anyone think of a time when using == 0 would cause problems instead of using == '0'?
NULL fields return true for == 0, but not for == '0', so I changed some code to use == 0.
The fields are ALL int fields so I don't think there could be a problem. I want == 0 to be true whether it's literally 0 or if it's null. So I don't see a problem. If it's greater than or less than zero obviously it will be false. If it's null or 0 it will be true. But I never had any sprt of formal training even on basic php so I'm being sure I'm not overlooking some issue.
I guess one would use '0' any time they want it to DEFINITELY be the number 0 and use 0 when "" or NULL could happen?
== 0
Started by BASHERS33, May 27 2009 02:20 AM
5 replies to this topic
#1
Posted 27 May 2009 - 02:20 AM
|
|
|
#2
Guest_Jordan_*
Posted 27 May 2009 - 04:14 AM
Guest_Jordan_*
If you want to test to make sure it is 0 you should test for type and value using ===:
More: Practical PHP Programming | TuxRadar
PHP: Type Juggling - Manual
$myInt = 0;
if ($myInt === 0) {
// true, it is an int and is 0
}According to the PHP Manual, Null is null when "it has not been set to any value yet." Setting it to 0, IMO, is a value. However, when I do the test:
$myInt = 0;
if ($myInt == NULL) {
echo "true";
}
It does equal true. Using the is_null() function does not return true:<?php
$myInt = 0;
if (is_null($myInt)) {
echo "true"; // Never happens
}I'd say you probably should use this for proper coding:
<?php
$myInt = 0;
if (is_null($myInt) || $myInt === 0) {
// Code Here for null or 0 value
}
As for your other question about '0' being (int) 0:Quote
As PHP is loosely typed, it will automatically convert one type to another whenever possible. Problems with automatic conversion occur when either no meaningful conversion is possible, or when conversion yields unexpected results. For example, calling "print" on an array makes PHP print out "Array"; it doesn't automatically convert the array to a string of all its elements. Treating an object like a string has its own unique behaviour, but that's not too important right now.
PHP: Type Juggling - Manual
#3
Posted 27 May 2009 - 05:34 AM
Thanks. I'll look over that, but in my situation I just don't see the point to checking if it's null. In the program it makes no difference whether it's null or zero. The only reason it could be one or the other is because my rows did have zero for defauult, but I changed it to null by default, so some rows may have null and some may have 0, but they both mean the same thing. Once an update is done they will always be greater than zero. So null or zero both mean "hasn't been updated yet" which is all the check is used for.
I could add is_null() if that really matters, but isn't it pretty much semantics? I would do it right off if not for the fact that I have to do it in a couple hundred places and so I want some benefit if I go to that troubke and in this way of me using it wouldn't it make no difference? Whether zero, nukll, or an empty string I am checking for the same thing and all three are the same in my situation thus why I am not bothering checking type.
This all goes back to me not understanding null. I am using it to save space and not for any other reason. I can do everything properly with no fields allowing nulls, but people confused me and I thought it will save space to have defaults as null for fields which won't ever be changed.
Some people say to only use null if a value may be added later, but my mom (used to be a DBA) said that's not true. That you do it if you don't want some operation done on a field with zero if the zero does not mean it;s known to be nothing, but rather you want it to mean nothing. And then she said in her experience it saves DB space in many cases.
I could add is_null() if that really matters, but isn't it pretty much semantics? I would do it right off if not for the fact that I have to do it in a couple hundred places and so I want some benefit if I go to that troubke and in this way of me using it wouldn't it make no difference? Whether zero, nukll, or an empty string I am checking for the same thing and all three are the same in my situation thus why I am not bothering checking type.
This all goes back to me not understanding null. I am using it to save space and not for any other reason. I can do everything properly with no fields allowing nulls, but people confused me and I thought it will save space to have defaults as null for fields which won't ever be changed.
Some people say to only use null if a value may be added later, but my mom (used to be a DBA) said that's not true. That you do it if you don't want some operation done on a field with zero if the zero does not mean it;s known to be nothing, but rather you want it to mean nothing. And then she said in her experience it saves DB space in many cases.
#4
Posted 27 May 2009 - 02:41 PM
NULL, 0, '0', "0", and FALSE are all different even though in PHP will consider them the same. It is bad practice to rely on type inference, because under different configurations or environments PHP may consider 0 and "0" different.
Jordan, you do know about var_dump, don't you?
Jordan, you do know about var_dump, don't you?
#5
Guest_Jordan_*
Posted 27 May 2009 - 03:08 PM
Guest_Jordan_*
Yup, I did but I wasn't thinking along those lines at the time :).
#6
Posted 27 May 2009 - 04:01 PM
Well at this point I am just rushing to be done with this for now. It's a neverending project and I have no idea if I will ever sell it. I just know it's working for me personally and at this point if I sell it then it's probably only going to be to people with the same php and mysql setup as me anyway (or close to the same),
But thatnks because I do need to know the info where I will program in a way which considers what you mentioned. I may go back and change this too. Blah. I don't know. It would only take 10 or 20 minutes, but it just seems kind of pointless when this is a php program so how it behaves in php is all that mattters for this particular program. If I want to have everything perfect coding well good luck to me because I never even learned programming formally, but by seeing examples and asking questions. So tons of things could be done "better", but they do work the way I have them.
I'm still beyond confused with NULL anyway and I'm not sure that I should even be using it. People stopped really answering and some people have told me it's best to avoid NULL when possible. Well for me I don't "need" NULL anywhere so I'm wondering if I wasted time changing tons of fields to it.
But thatnks because I do need to know the info where I will program in a way which considers what you mentioned. I may go back and change this too. Blah. I don't know. It would only take 10 or 20 minutes, but it just seems kind of pointless when this is a php program so how it behaves in php is all that mattters for this particular program. If I want to have everything perfect coding well good luck to me because I never even learned programming formally, but by seeing examples and asking questions. So tons of things could be done "better", but they do work the way I have them.
I'm still beyond confused with NULL anyway and I'm not sure that I should even be using it. People stopped really answering and some people have told me it's best to avoid NULL when possible. Well for me I don't "need" NULL anywhere so I'm wondering if I wasted time changing tons of fields to it.


Sign In
Create Account


Back to top









