Today in php why
<?
$a = "3d98";
$a++;
$a++;
$a++;
print $a;
guess what it outputs
@charlotte most probably 6, but it could also try to interpret this value as a hex, which would result in 3d9b
@charlotte I wouldn't call my reply something reasonable tbh
@charlotte I just tried it, it is quite funny, and one of the reasons to use stronger typed languages.
okay so lets step through this together
<?
$a = "3d98"; // a = "3d98"
$a++; // "3d99"
$a++; // "3e00"
$a++; // "4"
@charlotte Oh my. There needs to be a better symbol for hexidecimals so this doesn't happen.
@charlotte implicit number parsing: not even once.
also forget PHP in particular.
@charlotte Your favourite PHP apologist here.
The answer is 4 and this makes total sense:
3d99
3f00 (this is a float now)
4
Don’t increment strings if you don’t know what you’re doing.
@SuperDicq yes given the rules php uses for incrementing strings this is in fact consistent with them. however these rules are also utter bollocks, especially since $a += 1; behaves completely differently
@charlotte Yes, thia doesn’t work because summation is not the same thing as incrementing. Otherwise other sums, like += 2 or anything else would be very slow if they had to go through the entire increment logic.
@charlotte In C incrementing chars is just based on ASCII index. PHP is different and only supports alphabetical letters. “!” will stay “!” because it is unsupported loke what did you want “!” to turn into anyway?
This has the benefit that “Z” will become “AA” when you increment it which is in my opinion a lot more intuitive and useful than C where “Z” will increment to “‘“.
@SuperDicq if you increment a string in C, you will get a string with the first character missing (if the string isn’t empty, otherwise Fun stuff happens). it is consistent with pointer rules, since c strings are just pointers
anyways another identity that you might expect from incrementing that the result is approximately the same if you increment something, then decrement it
However decrementing a string seems to be a no op?
@SuperDicq mathematically speaking, the $a++ operator corresponds to the successor function, and the $a-- operator corresponds to the predecessor function. Addition and subtraction corresponds to repeated application of the successor and predecessor functions respectively (just like how multiplication and exponentiation is introduced as repeated addition and multiplication respectively)
@charlotte Yes, in PHP strings can only be incremented and not decremented. Why? I actually don’t know
If I take an educated guess it probably has to do with the fact that it’s not obvious what the string “a” should become after decrement or something like that.
@SuperDicq just like how it’s not obvious what the string “a” should become after increment. other stringly typed languages with increment operators, like …javascript, converts the string to a number (NaN if not a number) and increments that which is also amazingly inconsistent since adding a number and a string turns the number into a string
@charlotte “a” should definitely become “b” after increment, you learned the alphabet song in preschool.
Javascript is stupid and makes no sense.
@SuperDicq @charlotte u do realize ur calling smth stupid while defending smth stupid right
@emilis @charlotte No, I think PHP’s weak typing system makes sense once you understand it. I think Javascript’s weak typing system is not intuitive at all.
@SuperDicq even if you go ahead and define increment as “increment the alphabetic value (or whatever) of a string by one”, php’s string increment only works for a single alphabet. incrementing “α” results in “α” and not “β”
@charlotte Yeah, currently only numbers and the Latin alphabet is supported by increment function. I wouldn’t be opposed to adding more alphabets to it.
@SuperDicq also this still ignores the elephant in the original post that php mixes 4 different ways of how you could implement incrementing of character sequences simultaneously
@SuperDicq anyways i personally think this entire operation is meaningless. if creating sequences like in alphabetically-labeled numbered lists is that important it should just be a standard library feature instead. strictly typed languages do the correct thing and say that such an operation isn’t possible
@SuperDicq @charlotte my brother in Christ by that logic even undefined behavior makes sense if you know what it’ll do in that case on that platform/compiler
@SuperDicq also i just checked perl and i see where php got that particular can of brainworms from
@SuperDicq i also personally really like how adding partially numeric strings causes a warning, and adding non-numeric strings causes an error, yet there is absolutely nothing for something that is almost certainly a bug if it appears in production code
@charlotte Yes, that’s true. PHP’s increment is implemented in the same way it is in Perl.
@charlotte Addition is not the same operation as incrementing.
@emilis @charlotte Undefined behavior should be avoided obviously. This PHP example is defined and documented behavior.
@SuperDicq Addition is repeated incrementing. if you do addition with 1 you increment once. that is the same as regular incrementation
@SuperDicq @emilis UB in C and behavior like this still comes from the same mindset that the programmer is always right and makes no mistakes, and that it’s better to just do something completely unpredictable than it is to stop the program with an error message
@charlotte Addition is not repeated incremented. This is true even on an assembly level where there’s INC and ADD.
If you were to implement addition as repeated incrementing it would be much slower.
@SuperDicq like yes you could in principle use this in production code. but unless everyone who will read your code has a deep understanding in how the increment operator in php works, you would have to add a large description of what this single operation does, and at that point you can create a function named something like next_alpha_ordinal which would make more sense to see in code.
@SuperDicq @charlotte how did I expect this exact response lmao
ur to the point where ur saying if its documented its fine, there’s no point
@SuperDicq the end result is the same however. adding 5 to 1 is the same thing as incrementing 1 5 times
@charlotte If you work on large projects with other programmers who are not familiar with PHP’s weak typing system I would recommend to use declare(strict_types=1);. This also removes the ability to increment strings I believe.
@SuperDicq ah yes the little brother to “those who do not implement static typing are doomed to add it later”: “those who do not implement strict typing are doomed to add it later”
@charlotte I think PHP is just very flexible with optional features like this. Very inclusive to everyone from many different backgrounds, right?
@SuperDicq @charlotte You know what else is very flexible and can be used for lots of different things?
Lead!
also what the fuck is up with that last sentence jfc, using a language that will shoot ur foot first chance it gets unless u know the arcane wisdomsTM is inclusive of just about nothing but the people with too much invested in it to let go