Edit report at https://bugs.php.net/bug.php?id=39579&edit=1
ID: 39579 User updated by: iain at workingsoftware dot com dot au Reported by: iain at workingsoftware dot com dot au Summary: Comparing zero & string values in boolean comparison has unexpected behaviour Status: Not a bug Type: Bug Package: Variables related Operating System: FreeBSD 6.1 PHP Version: 5.2.0 Block user comment: N Private report: N New Comment: Hi, I agree this shouldn't be changed. It's pretty fundamental to how PHP works and since reporting this bug 6 years ago I've learned a lot more about PHP :) I still think my suggestion of emitting a Notice whenever a string is converted to 0 as a result of being evaluated in a numeric context isn't too outlandish though. *Changing* the way that strings are evaluated would break a buttload of code and isn't really productive. Emitting a Notice would help people unfamiliar with the implicit typecasting behaviour save some time and avoid bugs when developing. For those that like to write "clean code" with no Notices there is a very simple way of suppressing it (ie. use === or (int)). Also, emitting a notice wouldn't actually break any code, although it might cause a number of scripts out in the wild to become more verbose in their logging - is that not an acceptable risk? The notice could even say something like: Notice: String evaluated to 0 (zero) when used in numeric context on line WHATEVER. Use === or (int) to prevent this notice. This would enable people to quickly understand the behaviour (which is kind of unintuitive but fair enough when you look at PHPs type system - just a foible of the language everyone should learn) and encourage people to learn the importance of === earlier on. Previous Comments: ------------------------------------------------------------------------ [2013-02-14 17:26:40] radamanf at gmail dot com I've seen a topic in the web: http://josephscott.org/archives/2012/03/why-php-strings-equal-zero/ related to this bug, and I can see that sometimes string need to be compared with integer as integer BUT this is a huge pool of potential bugs across all the worldwide! :) Can you imagine how many people usign PHP without realizing this BUG ! :)_) I'm sure this is a loved BUG by most hackers out there ! ------------------------------------------------------------------------ [2013-02-14 17:24:51] ni...@php.net @radamanf: Stop shouting. Shouting does not help. I agree that this behavior ('foo' == 0) is counter-productive. I think most people would agree on that. But even if everyone agrees that it's the wrong behavior, changing it isn't so easy. Changing this behavior will probably break existing software. If you really want to change this, then there is only one way: Make the change and then test a shitload of code against it. See how many tests will fail in major PHP projects and how easy things are to fix. If you can provide sufficient data that this change (which goes rather deep into the core semantics of the language) won't affect existing projects heavily, then I see no problem with doing it. ------------------------------------------------------------------------ [2013-02-14 17:14:35] radamanf at gmail dot com Related To: Bug #39579 ------------------------------------------------------------------------ [2013-02-14 17:14:35] radamanf at gmail dot com This is a epic FAIL in pure logic of comparing formats not having explicitly defining them, whole beauty of PHP disappear! This is BROKEN LOGIC. Connected BUGS Bug #44990 Bug #39579 Different people are coming across this BUG and thinking the same as me, so please CHANGE your documentation, it's WRONG! Who is this "GENIES" to make possible converting String to Integer NOT INT TO STRING! Guys, I'm very disappointed! :) ------------------------------------------------------------------------ [2012-08-01 10:31:16] v dot picture at free dot fr Hi, I'm wondering why this comparison should be evaluated in a numeric context and not a string context, after all there is no loss with string casting whereas there is a huge risk of doing mistakes with numeric casting: (string) 0 => "0" (string) 42.5 => "42.5" (int) "test" => 0 But ok, let's say it's a normal behavior. "If you compare a number with a string or the comparison involves numerical strings, then each string is converted to a number" Then why would PHP decide to do that in a string context ? I mean, when I compare two strings I don't expect PHP to convert everything to numbers ! "10" == "1e1" => true Sorry folks, this really seems like a string context to me. ------------------------------------------------------------------------ The remainder of the comments for this report are too long. To view the rest of the comments, please view the bug report online at https://bugs.php.net/bug.php?id=39579 -- Edit this bug report at https://bugs.php.net/bug.php?id=39579&edit=1