• some guy (unregistered)

    Or was it someone's frist attempt at validating that the variable is stringly-typed?

  • Hanzito (unregistered)

    I can find one possible reason: if strings are mutable in this language (PHP?). Then you'd want a copy.

  • Alistair (unregistered)

    The substringing may do something interesting if $selectid has length 0.

  • L (unregistered)

    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

  • Alex (unregistered)

    Am I the only one seeing strlen($selectid)-1, meaning it cuts off one character from the end?

  • Tinkle (unregistered)

    A promise to give you the string, the whole string and nothing but the string?

  • Maia Everett (github)

    Am I the only one seeing strlen($selectid)-1, meaning it cuts off one character from the end?

    This seems correct. The third parameter of PHP's substr is length, 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

  • (nodebb) in reply to Alex

    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

  • Richard Yates (unregistered) in reply to L

    That PHP code returns the string with the last character stripped off, not the whole string.

  • (nodebb)

    At first glance I thought "Oh, another Perl one..." but then I realized "Oh, it's the other one with P". :-)

  • (nodebb)

    The thing that jumped out at me was the combination of snake_case, camelCase, and runonsentencecase.

  • Tinkle (unregistered) in reply to Tinkle

    Or not, as this case may be...

    It has been a long while since I did any string manipulation, fortunately.

  • Officer Johnny Holzkopf (unregistered)

    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"...

  • Simon (unregistered)

    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.

  • Darren (unregistered) in reply to Simon
    Comment held for moderation.
  • (nodebb) in reply to Simon

    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 Python range()) use it as an exclusive index, so you don't have to subtract 1.

  • (nodebb)

    If it is actually removing the last character (see the debate above), my guess was they were stripping of a newline character.

  • (nodebb)
    Comment held for moderation.
  • (nodebb) in reply to Alex

    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.

  • (nodebb)

    COnsidering the number ofposts about stripping a char, I feel I must remind everyone of The Two Most Common Errors In Programming

    1. Buffer Overflow
    2. failure to initialize
    3. Off-By-One errors
  • Randal L. Schwartz (github) in reply to MaxiTB

    At first glance I thought "Oh, another Perl one..." but then I realized "Oh, it's the other one with P". :-)

    PHP has always just been a Perl wannabe. Never quite catching up, and losing ground over the years as Perl continues to advance.

Leave a comment on “The Whole Thing”

Log In or post as a guest

Replying to comment #:

« Return to Article