Search - "strpos() !== false"
  • 7
  • 5
    A bit longer rant, somehow triggered by the end of this rant:

    https://devrant.com/rants/7145365/...

    The discussion revolved around strpos returning false or a positive integer.

    Instead of an Option or a Exception.

    I said I'm a sucker for exception, but I'm also a sucker for typing.

    Which is something most languages lack - except the lower level ones like C / C++.

    I always loved languages which have unsigned and signed types.

    There, I said it... :) I know that signed / unsigned is controversial, Google immediately leads to blog entries screaming bloody murder because unsigned can overflow – or underflow, if someone tries to use a -1on an unsigned integer.

    Note that my love is only meant for numeric types, unsigned / signed char is ... a whole can of insanity on its own.

    https://phoronix.com/news/...

    If you wanna know more.

    Back to the strpos problem, now with my secret love exposed:

    strpos works on a single string, where a string is a sequence of chars starting with 0.

    0 is a positive integer.

    In case the needle (char that should be looked up in the string) cannot be found in the haystack (the string), PHP returns "false".

    This leads to the necessity of explicitly checking the type as "0" (beginning of string, a string position)... So strpos !== false.

    PHP interprets 0 as false, any other integer value is true.

    In the discussion, the suggestion came up to return -1 if a value could not be found – which some languages do, for example Scala.

    Now I said I have a love for unsigned & signed integers vs. just signed integers...

    Can you guess why the -1 bothers me very much?

    Because it's a value that's illogical.

    A search in a sequence that is indexed by 0 can only have 0 or more elements, not less than zero elements.

    -1 refers to a position in the sequence that *cannot* exist.

    Which is - of course - the reason -1 was chosen as a return value for false, but it still annoys me.

    An unsigned integer with an exception would be my love as a return value, mostly because an unsigned integer represents the return value *best*. After all, the sequence can only return a value of 0 ... X.

    *sigh*

    Yes, I know I'm weird.

    I'm also missing unsigned in Postgres, which was more or less not implemented because it's not in the SQL standard...

    *sob*
    30