- Feature Articles
- CodeSOD
- Error'd
-
Forums
-
Other Articles
- Random Article
- Other Series
- Alex's Soapbox
- Announcements
- Best of…
- Best of Email
- Best of the Sidebar
- Bring Your Own Code
- Coded Smorgasbord
- Mandatory Fun Day
- Off Topic
- Representative Line
- News Roundup
- Editor's Soapbox
- Software on the Rocks
- Souvenir Potpourri
- Sponsor Post
- Tales from the Interview
- The Daily WTF: Live
- Virtudyne
Admin
Or was it someone's frist attempt at validating that the variable is stringly-typed?
Admin
I can find one possible reason: if strings are mutable in this language (PHP?). Then you'd want a copy.
Admin
The substringing may do something interesting if $selectid has length 0.
Admin
wait it's actually strlen - 1, so it strips off the last char (or PHP is fucked like that.)
btw I expected some comments on the mixed-case stuff instead
Admin
Am I the only one seeing strlen($selectid)-1, meaning it cuts off one character from the end?
Admin
A promise to give you the string, the whole string and nothing but the string?
Admin
This seems correct. The third parameter of PHP's
substr
islength
, so it should strip off the last character (though the more idiomatic way of doing this is to just specify -1 as the third parameter).https://www.php.net/manual/en/function.substr.php
Admin
You're not the only one (L posted about it before you), and yes, it does, indeed, lose the last character of the string.
And If the original string is empty, there will be something interesting going on, but the behaviour is uncharacteristically well-defined, returning an empty string. I think. The doc page is ... unclear ... on this case.
https://www.php.net/manual/en/function.substr.php
Admin
That PHP code returns the string with the last character stripped off, not the whole string.
Admin
At first glance I thought "Oh, another Perl one..." but then I realized "Oh, it's the other one with P". :-)
Admin
The thing that jumped out at me was the combination of snake_case, camelCase, and runonsentencecase.
Admin
Or not, as this case may be...
It has been a long while since I did any string manipulation, fortunately.
Admin
At least the line presented is very representative in coding styles for identifiers: "model_sale_manageorder" with underscores, "exportOrder" in camel case and "selectid" with nothing at all. Maybe a few lines above or beyond, there's "CustomerNumber" in Pascal case, TOTAL in all capitals, and "lpnszFirstName" and "iAmount"...
Admin
The code does not lose the last character of the string!
Given "substr($selectid,0,strlen($selectid)-1))" it’s clear that in this language the first character of a string is at index 0, so if the string is "bar" with length 3, the index of the "r" is 2, i.e. strlen("bar") - 1.
Remy’s note "we take the substr of $selectid from 0 to strlen($selectid)- aka, we take the entire string" would read beyond the end of the string.
Admin
I thought the same thing - that'd it just return the string rather than drop the last character. The other comments, and never having written PHP, did make me doubt myself for a while.
Admin
The third argument is the length, not the ending index, so you don't have to subtract 1.
And in any sane language, functions that take an end index (e.g. JavaScript
String.slice()
, and Pythonrange()
) use it as an exclusive index, so you don't have to subtract 1.Admin
If it is actually removing the last character (see the debate above), my guess was they were stripping of a newline character.
Admin
Umm... you missed the -1 in the 3rd argument passed to
substr
. Perhaps this would've been better though:Admin
You're correct. PHP's substr uses index and length (rather than begin and end indices) so the last character will be cut off.
Addendum 2025-01-14 12:31: Apparently my response was redundant; I had not refreshed before posting.
Admin
COnsidering the number ofposts about stripping a char, I feel I must remind everyone of The Two Most Common Errors In Programming
Admin
PHP has always just been a Perl wannabe. Never quite catching up, and losing ground over the years as Perl continues to advance.
Admin
That was a new one for me. My two things were:
Admin
While I don't speak PHP I agree with the crowd that this is stripping a terminator and thus is not a WTF. But there is a WTF here--why is there no path that creates it without putting the terminator on in the first place? Why should a terminator-stripper exist outside of some sort of parser?
Admin
The weird casing is php "best" practices, per PSR-1. The snake_case is not strictly necessary for property names but it's allowed (and not uncommon in the wild). Runonsentencecase is not normal though!
Admin
No, the four hardest problems are
Segementation violation. Core dumped