4 ms·
This, combined with the fact that you can increment strings gives some 'interesting' results: $a = "2d9"; $a++; echo $a . "\n"; $a++; e
by lars 11y ago
This, combined with the fact that you can increment strings gives some 'interesting' results:
$a = "2d9";
$a++;
echo $a . "\n";
$a++;
echo $a . "\n";
Output
2e0
3
- ulrikrasmussen 11y agoThis is wonderful, I love it! Interestingly [1], this echoes "2e0" followed by "3" in hhvm-3.7.0, but "3" followed by "4" in hhvm-3.6.0. [1] http://3v4l.org/sJhP8 http://3v4l.org/sJhP8
- debacle 11y agoThat means that someone was using this "feature" in a relatively core piece of code from the PHP ecosystem. Enough that hhvm felt they needed to support it.
- jlouis 11y agoThere is some nasty type conversion going on here, from the type of stochastic random throws of two nine-sided dice to floats to integers. Where is your type preservation, PHP?
- jlouis 11y agoThere is some nasty type conversion going on here, from the type of stochastic random throws of two nine-sided dice to floats to integers. Where is your type preservation, PHP?
- marcofiset 11y agoWho tries to increment strings anyway? What is your point here?
- pionar 11y agobecause PHP is dynamically typed, it's easier to accidentally increment a string.
- deleted 11y ago[deleted]
- mikeash 11y agoGiven how happy PHP is about converting strings to integers on demand, it would be pretty easy to take string input intended to be a number, forget to actually convert it, and go around using it happily until one day you accidentally set off a bomb.
- BasDirks 11y agoYeah, who would do that? Nobody. So why the _ is it possible in the first place?
- Someone1234 11y agoIs there any way to defend against this one? I know === to turn off type conversion with the equality operator, but what about here?
- digitalzombie 11y agoIf condition check type and not try to increment a string... if (!is_string($notstr)) ++$notstr; edit: I think checking for integer is better, if it's just incrementing integer. I was going to say type hinting but I just realized php's primitive cannot be type hinted.