4 ms·
> It doesn't make sense unless you understand how PHP works ... In other words: equality is not intuitive. Awesome! --- Let's take a look at "how PHP works":
by jessedhillon 15y ago
> It doesn't make sense unless you understand how PHP works ...
In other words: equality is not intuitive. Awesome!
---
Let's take a look at "how PHP works":
0 == 08 // true!
WTF, really? Well, let's just use the magical 'I-mean-actual-equality-not-some-other-kind-of-equality' operator:
0 === 08 // also true!
(Sonofabitch/Facepalm) * Infinity
Of all the areas of a language in which one could gain expertise in, I think testing equality should not be one of the more difficult to master.
This is just stupid behavior. 08 is an invalid octal sequence, but no error is raised. You can't detect this condition unless you do your own pre-parsing before allowing PHP to try to parse it!
- wvenable 15y agoI've been programming in PHP for a long time and I've never ever accidentally used an octal anywhere. Does such a non-problem really need this much attention?
- jessedhillon 15y agoPerhaps you ask a user to enter the month of their CC expiration date, which is shown as 08/14 on their card. $v = validated_integer($user_input); // the user input 08 if($v == 0 || $v === 0) { // let's just be safe // the Wrong Thing happens } This is a contrived example, sure. But you've never checked to see if a user input the number 0? Or a non-zero, positive integer?
- wvenable 15y agoI'm not sure what you expect to happen there but the following code always produces "Correct!": $user_input = '08'; $user_input = (integer)$user_input; if ($user_input == 8) echo 'Correct!'; if ($user_input == 0) echo 'Incorrect!'; If you take out the cast, the result is the same. If you change the numbers to '010' and 10 respectively, the result is also "Correct!". There is no weirdness.
- DisposaBoy 15y agoThat's not a PHP issue. It's a programmer issue. Validate the bloody input properly! e.g if someone types in a phone number with hyphens or a credit card number which may contain spaces, don't blindly check it for a number...
- bnr 15y agoThat's the grammar for PHP integer literals[1]: decimal : [1-9][0-9]* | 0 hexadecimal : 0[xX][0-9a-fA-F]+ octal : 0[0-7]+ integer : [+-]?decimal | [+-]?hexadecimal | [+-]?octal "08" and "09" do not match any of these. php -r "var_dump(08);" Yields "int(0)", while the case should rather be treated as a syntax error, if you ask me. The bottom line probably is that your literals just shouldn't have leading zeroes… [1] http://www.php.net/manual/en/language.types.integer.php http://www.php.net/manual/en/language.types.integer.php