Re: [PHP] = 0 and = "0"
On Thu, 25 Oct 2001 02:04:14 -0700 (PDT) impersonator of [EMAIL PROTECTED] (Rasmus Lerdorf) planted &I saw in php.general: > if(strpos("abcdef","abc")) { ... } > >and then be confused that the condition appeared not to be met. What they >actually should have done was: > > if(strpos("abcdef","abc")!==false) { ... } > Or better yet, i think: if(is_int(strpos(..))){..} coz this way it should not depend on php version (whereus === and !== do depend), i think:) -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]
RE: [PHP] = 0 and = "0"
Hi John! > > > The integer 0 is equal to False, but not Null. > > > > Not quite true. 0 evaluates to false. They are not equal. > Try this: > [...] > > So be careful and try to think of 0 and false as distinct > and separate > > entities and you will avoid coding mistakes like this. > > Is this seen as a strength or a weakness of PHP.? According to my experience I'd say both, depending on the context... If you have to determin its type before using a variable and then, when changing it, have to do an explicit typecast, programming gets "cleaner" and in many cases more safe. On the other hand, if let's say false == 0 and true == 1, you can do nice things like arithmetic operations using booleans: $x = $y * (-1 * (abs($z) == $z)); Get the idea? > In C(++), direct comparisons to true/false are discouraged. > In other words, instead of this: > bool x; > if(x==true) ... > if(x==false) ... > > itt's always considered better to do this: > if(x) ... > if(!x) ... > > But PHP seems to require a direct comparison with false > in certain situations. Are there any cases where you need > to do a direct comparison with true? AFAIK it's only a matter of speed. Unary operators consume less time than binary ones... Correct me if I'm wrong. > Are you coming to Ottawa in the future? This I don't know... ;) Cheers, Kiko - It's not a bug, it's a feature. christoph starkmann mailto:[EMAIL PROTECTED] http://www.fh-augsburg.de/~kiko ICQ: 100601600 - -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]
Re: [PHP] = 0 and = "0"
"Rasmus Lerdorf" <[EMAIL PROTECTED]> wrote in message [EMAIL PROTECTED]">news:[EMAIL PROTECTED]... > > The integer 0 is equal to False, but not Null. > > Not quite true. 0 evaluates to false. They are not equal. Try this: [...] > So be careful and try to think of 0 and false as distinct and separate > entities and you will avoid coding mistakes like this. Is this seen as a strength or a weakness of PHP.? In C(++), direct comparisons to true/false are discouraged. In other words, instead of this: bool x; if(x==true) ... if(x==false) ... itt's always considered better to do this: if(x) ... if(!x) ... But PHP seems to require a direct comparison with false in certain situations. Are there any cases where you need to do a direct comparison with true? Are you coming to Ottawa in the future? -- John A. Grant * I speak only for myself * (remove 'z' to reply) Radiation Geophysics, Geological Survey of Canada, Ottawa If you followup, please do NOT e-mail me a copy: I will read it here -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]
Re: [PHP] = 0 and = "0"
Check the link I posted. http://24.234.52.166 There is the code I wrote and it's output. The two lines print. You can explain me why tomorrow they print on my server and not yours. Good night > > But his two lines still don't need typecasting, they both work: > > http://24.234.52.166 > > I don't mean to pick on you, but no, you are wrong. And I am only doing > this because so many people get confused on this point and I always see > questions related to it. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]
Re: [PHP] = 0 and = "0"
> You are right, "0" didn't show as a value. > > But his two lines still don't need typecasting, they both work: > http://24.234.52.166 I don't mean to pick on you, but no, you are wrong. And I am only doing this because so many people get confused on this point and I always see questions related to it. His lines were: $qty = "0" ; if ($qty != "test") print "qty is not test"; $qty = 0 ; if ($qty != "test") print "qty is not test"; So his two conditions can be simplified to: if("0"!="test") echo 1; if(0!="test") echo 2; Try this code yourself, or his original code if you want. Only the first condition is met and you will only see it print 1. When you are comparing an integer to a string, the string will get converted to its integer representation. The integer representation of "test" is 0. So the second condition becomes: if(0!=0) which is obviously false. Therefore if you cast it: if((string)0!="test") You are again comparing a string to a string and PHP will not try to convert "test" to an integer. -Rasmus -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]
Re: [PHP] = 0 and = "0"
> The integer 0 is equal to False, but not Null. Not quite true. 0 evaluates to false. They are not equal. Try this: if(0==false) echo "1"; if(0===false) echo "2"; You will find that this only prints "1". 0 and false are distinct and separate values. This is something that confuses a lot of people, especially in conjunction with something like the strpos() function which will return 0 when it matches something at position 0 in the string, but it returns false when there is no match. A lot of people will mistakenly do: if(strpos("abcdef","abc")) { ... } and then be confused that the condition appeared not to be met. What they actually should have done was: if(strpos("abcdef","abc")!==false) { ... } So be careful and try to think of 0 and false as distinct and separate entities and you will avoid coding mistakes like this. -Rasmus > > Robin > > Kodrik wrote: > > > > > > If you set $qty=0; then $qty has no value. > > > > > > Of course it has a value. > > > > No it doesn't have a value. > > PHP interprets 0 as null. > > > > A very easy way for you to check: > > > > $value=0; > > > > if(!$value) printf("$value doesn't have a value (it didn't even print > > 0)\n"); > > > > $value="0" > > if($value) printf("$value does have a value if I put "0" instead of 0\n"); > > > > -- > > PHP General Mailing List (http://www.php.net/) > > To unsubscribe, e-mail: [EMAIL PROTECTED] > > For additional commands, e-mail: [EMAIL PROTECTED] > > To contact the list administrators, e-mail: [EMAIL PROTECTED] > > -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]
Re: [PHP] = 0 and = "0"
You are right, "0" didn't show as a value. But his two lines still don't need typecasting, they both work: http://24.234.52.166 -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]
Re: [PHP] = 0 and = "0"
Kodrik, you are picking the wrong person to argue with. ;) > > > If you set $qty=0; then $qty has no value. > > > > Of course it has a value. > > No it doesn't have a value. > PHP interprets 0 as null. Completely incorrect. > A very easy way for you to check: > > $value=0; > > if(!$value) printf("$value doesn't have a value (it didn't even print > 0)\n"); if() doesn't check for null, it checks for a boolean false condition. Does 0 evaluate to false? Sure it does. So does "" and "0". That doesn't mean they are null. > $value="0" > if($value) printf("$value does have a value if I put "0" instead of 0\n"); That is completely wrong as well. You obviously didn't try your own example. The following all evaluate to false: if(0) if(false) if(null) if("0") if("") But the fact that they all evaluate to false says nothing about whether they are values or not. Just like all of these all evaluate to true: if(1) if(true) if("abc") if(-1) That doesn't mean that 0 == null anymore than it means that 1 == -1. Just because two values both evaluate to the same boolean state does not mean they are one and the same. -Rasmus -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]
Re: [PHP] = 0 and = "0"
The integer 0 is equal to False, but not Null. Robin Kodrik wrote: > > > > If you set $qty=0; then $qty has no value. > > > > Of course it has a value. > > No it doesn't have a value. > PHP interprets 0 as null. > > A very easy way for you to check: > > $value=0; > > if(!$value) printf("$value doesn't have a value (it didn't even print > 0)\n"); > > $value="0" > if($value) printf("$value does have a value if I put "0" instead of 0\n"); > > -- > PHP General Mailing List (http://www.php.net/) > To unsubscribe, e-mail: [EMAIL PROTECTED] > For additional commands, e-mail: [EMAIL PROTECTED] > To contact the list administrators, e-mail: [EMAIL PROTECTED] -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]
Re: [PHP] = 0 and = "0"
Thank you, that was it. I needed to test the variable against both an integer and a string, so I ended up using (string) when I need to compare a string. Rest of the time, it's an integer. Robin Rasmus Lerdorf wrote: > > > If you set $qty=0; then $qty has no value. > > Of course it has a value. The value is 0. > Quite distinct from not having a value, or in proper terms, not being set. > Try this: > > var_dump($qty); > $qty = 0; > var_dump($qty); > > Well, I will save you the trouble, it outputs: > > NULL > int(0) > > > Type casting is irrelevant in php. > > No they aren't. Try this: > > $qty = 0; > if($qty=="test") echo "foo"; > if((string)$qty=="test") echo "bar"; > > This will only output "foo". Not "bar". You could also use the === > operator to force PHP to check not only the value but also the type. > > See http://www.php.net/manual/en/language.operators.comparison.php > > -Rasmus > > -- > PHP General Mailing List (http://www.php.net/) > To unsubscribe, e-mail: [EMAIL PROTECTED] > For additional commands, e-mail: [EMAIL PROTECTED] > To contact the list administrators, e-mail: [EMAIL PROTECTED] -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]
Re: [PHP] = 0 and = "0"
> > If you set $qty=0; then $qty has no value. > > Of course it has a value. No it doesn't have a value. PHP interprets 0 as null. A very easy way for you to check: $value=0; if(!$value) printf("$value doesn't have a value (it didn't even print 0)\n"); $value="0" if($value) printf("$value does have a value if I put "0" instead of 0\n"); -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]
Re: [PHP] = 0 and = "0"
> If you set $qty=0; then $qty has no value. Of course it has a value. The value is 0. Quite distinct from not having a value, or in proper terms, not being set. Try this: var_dump($qty); $qty = 0; var_dump($qty); Well, I will save you the trouble, it outputs: NULL int(0) > Type casting is irrelevant in php. No they aren't. Try this: $qty = 0; if($qty=="test") echo "foo"; if((string)$qty=="test") echo "bar"; This will only output "foo". Not "bar". You could also use the === operator to force PHP to check not only the value but also the type. See http://www.php.net/manual/en/language.operators.comparison.php -Rasmus -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]
Re: [PHP] = 0 and = "0"
I just tested those two lines with php 4.0.6 and they both work. There is a difference though. If you set $qty=0; then $qty has no value. But if you set $qty="0", it has a value. Type casting is irrelevant in php. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]
RE: [PHP] = 0 and = "0"
The only reason I could see that not working is if PHP is typecasting "test" to (int) in the second example... $qty = "0"; (string) "test" = "test"; (string) "0" != "test" (evaluates true) $qty = 0; (int) "test" = 0; (when cast to int) 0 != 0 (evaluates false) e.g. By comparing an int to a string in the 2nd example, the string is casted to int, and the int cast of "test" is 0. -Original Message- From: Robin Chen [mailto:[EMAIL PROTECTED]] Sent: Thursday, October 25, 2001 4:06 AM To: [EMAIL PROTECTED] Subject: [PHP] = 0 and = "0" why does work properly but not the following Thanks, Robin -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED] -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]
Re: [PHP] = 0 and = "0"
Hello Robin, Very simple. :) RC> $qty is string, so PHP compares "0" with "test" char by char. chr("0") isn't equal "t". RC> In this case $qty is integer value, so PHP tries to convert "test" to integer value [ integer("test") == 0], so last condition result is FALSE. Is it clear? RC> Thanks, RC> Robin -- Best regards, Vitali Falileev mailto:[EMAIL PROTECTED] -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]
[PHP] = 0 and = "0"
why does work properly but not the following Thanks, Robin -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]