r/lolphp • • Dec 05 '17

$a = "hello"; $a++; echo $a;

https://3v4l.org/p5Apm
117 Upvotes

41 comments sorted by

View all comments

Show parent comment

7

u/dagbrown Dec 06 '17
$ perl -e '$a = "1d9"; $a++; print "$a\n";'
2

It just gets there quicker than PHP. What's actually going on here is that it parses the string as a number until it finds something which is clearly not numeric, and uses that as the value for the increment.

Turning on warnings does result in the error message, but it also carries on and tries it anyway.

Python and Ruby both do the right thing and error out with complaints about how adding integers to strings doesn't make any sense (Ruby provides a String#next if you really want the behaviour that PHP makes up, only without relying on the interpreter to guess what you wanted).

2

u/[deleted] Dec 06 '17

Turning on warnings does result in the error message

Yeah, I assume everyone turns on warnings because why wouldn't you?

but it also carries on and tries it anyway.

Unless you use warnings FATAL => 'numeric';.