The example I picked in my patch was a little contrived, however I do think it is a useful benefit for functions to work in ways people expect, even in edge cases. There are a lot of people out there who do not know the difference between == and ===, and I think the fact that str_slice() has one less potential bug to worry about is a tangible improvement.
As for adding other string functions, I agree, I think there are a lot of them that would be great to add. starts_with & ends_with for sure. I just didn't want to write a really large patch for a whole host of functions before having the discussion about this one. I also don't mean to attack PHP. I think the interface for this function is heavily borrowed from C, and I think because PHP has different goals than C (ease of use being one of them), that improvements can be made from this perspective. I think from a functionality point of view for an expert user, the C function interfaces are great. -Dan On Wed, Mar 30, 2011 at 7:43 AM, Jonathan Bond-Caron <jbo...@openmv.com>wrote: > On Wed Mar 30 09:05 AM, Hannes Landeholm wrote: > > > > var_dump(\substr("foo", 5, 6) == "", (string) false, false == ""); > > > > Welcome to PHP. To be honest this criticism pretty much falls in the > > "from person that comes from another language X and is annoyed that > > every little detail isn't exactly the same"-category. Just make your > > own substr() function that uses the behavior you expect if you don't > > like the native version. Although that's bad practice - the best > > solution is to get used to it. And if you have an urge to write about > > your experience with a new language I suggest you do it in a blog > > instead of posting it in the internals mailing list... > > I agree with what you're saying but I think you're being a little harsh, > experience with a new language should be something that matters to > internals. > > Back to Dan, you're hitting type conversion so not sure I understand "you > always have to deal with this FALSE case": > > var_dump(substr('', -1) == '0'); // TRUE > var_dump(substr('', -1) === '0'); // FALSE > > Equally to Dan, it doesn't seem like a great way to start the conversation > by saying the "interface is inconsistent and confusing". > There's a perception issue here since you're already used to using language > X,Y,Z. > > That said the patch seems to add value to php, why not add str_slice() if > it > can solve consistency issues for some users? str_slice likely could be a > faster alternative to substr() in userland parsers. > > Also I've seen plenty of bugs caused by type conversion & substr() in > userland > > >