3 ms·
PHP's type coercion is nothing like I have every seen in any other language. Its horrendously messy, ugly and completely inexcusable. Is it object
by WalterGR 12y ago
PHP's type coercion is nothing like I have
every seen in any other language. Its
horrendously messy, ugly and completely
inexcusable.
Is it objectively worse than type coercion in JavaScript?
- eurleif 12y agoPHP: var_export(0 == "hello"); // true JavaScript: console.log(0 == "hello"); // false
- deleted 12y ago[deleted]
- WalterGR 12y agoActually, I was hoping for something more than a single example. Or, did you mean that PHP and JavaScript were neck-and-neck all the way up to that one example, and ultimately it's the very one that proves PHP's type coercion is worse?
- eurleif 12y agoYou're in a thread about how PHP's type coercion can easily cause a serious vulnerability. So, the title of this thread is your second example. If you want a third example, find it yourself.
- WalterGR 12y agoGive me a break. 3 examples isn't enough to answer the question. Your comment history shows you ask questions in lieu of doing your own research. If you don't want to take the time, then move on.
- oisyn 12y agoOh yes. In Javascript, the operands are only coerced if one of the operands is a number. So when comparing two strings (regardless of whether the strings can be interpreted as a number), you always get a regular string compare. "12" == "12.0" -> false, basic string compare Furthermore, if one operand happens to be a number, and the other operand has illegal characters to be interpreted as a number, the two operands aren't equal. 0 == "foo" -> false, "foo" is not a valid number 12 == "12 monkeys" -> false, "12 monkeys" is not a valid number 12 == "12.0" -> true, "12.0" is a valid number, and compares equal to 12. In short, in Javascript the == operator actually makes sense. In PHP, every single one of above examples would evaluate to true.
- romaniv 12y agoWhile it is obvious that PHP's == operator is horrible, JavaScript has its share of pretty bad issues, like "x" - 1 giving NaN. What I don't understand is why some people agree that PHP is a horrible language, while at the same time praising JavaScript as messiah of scripting. These two languages don't just have problems, they have very similar problems. Moreover, they gained popularity for very similar reasons (lack of choice). Seriously, if you posted something similar to OP about JavaScript the first thing people would tell you is "What, you're still not using ===?!"
- deleted 12y ago[deleted]