- Feature Articles
-
CodeSOD
- Most Recent Articles
- Irritants Make Perls
- Crossly Joined
- My Identification
- Mr Number
- intint
- Empty Reasoning
- Zero Competence
- One Month
-
Error'd
- Most Recent Articles
- Not Impossible
- Monkeys
- Killing Time
- Hypersensitive
- Infallabella
- Doubled Daniel
- It Figures
- Three Little Nyms
- 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?
Edit 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
Edit 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.
Edit Admin
At first glance I thought "Oh, another Perl one..." but then I realized "Oh, it's the other one with P". :-)
Edit 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.
Edit 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.Edit Admin
If it is actually removing the last character (see the debate above), my guess was they were stripping of a newline character.
Edit 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.
Edit Admin
COnsidering the number ofposts about stripping a char, I feel I must remind everyone of The Two Most Common Errors In Programming
Edit Admin
PHP has always just been a Perl wannabe. Never quite catching up, and losing ground over the years as Perl continues to advance.