preg_replace('/.?$/s', "x", 'abc'),
preg_replace('/.$/s', "x", 'abc')
string(4) "abxx" <-- strange, isn't it?
preg_replace replaces all matches in the string. So after doing each replacement, it searches for another match starting after the place where it made the previous replacement.
So it first looks for
/.?$/ and finds
c, and replaces it with
x, resulting in
abx. Then, it looks again for
/.?$/ starting right after the
.? means to match 0 or 1 characters, it matches the zero-length string at the end of the string, and replaces it with
x. Then it stops because it has reached the end of the string.