• Araozu@lemmy.world
        link
        fedilink
        arrow-up
        20
        arrow-down
        5
        ·
        1 year ago

        How about “php enables me to code like a moron”, or even better, "php breaks common conventions and forces me to think about every little detail and special edge case, slowing me down if I don’t want to accidentally ‘code like a moron’ "

        Nested ternary operators emerge because of the lack of if/switch expressions (which is C fault), so they are “useful” (they shouldn’t be). However, PHP is the only language that treats it as left associative. This has 2 problems:

        • You are forced to use parenthesis. Some (insane) people might do: (cond1) ? “A” : (cond2) ? “B” : “C” And it makes sense. Its ugly af, but it makes sense. But PHP now forces you to use more parethesis. It’s making you work more.
        • It breaks convention. If you come from any other language and use ternary operators, you will get unexpected results. After hours of banging your head against the wall, you realize the problem. And now you have to learn a new edge case in the language, and what to do to actually use the language.

        “But you shouldn’t use ternary operators anyway! Use if/switch/polymorphic dispatch/goto/anything else”

        True, but still, the feature is there, and its bad. The fact that there are other alternatives doesn’t make the PHP ternary operator worse than other languages’ ternary operator.

        PHP works against you. That’s the problem. The ternary operator is not a good example, since there are alternatives. But look at something so simple, so mundane like strpos.

        If strpos doesn’t find returns false. Every other language returns -1. And if you then use this value elsewhere, PHP will cast it to 0 for you. Boom, your program is broken, and you have to stare at the screen for hours, looking for the error.

        “BuT yOU sHoUlD AlwAyS cHEcK tHe rETurN eRRor!”

        And even if that’s true, if we all must check the return value, does PHP force you to do so? Like checked exceptions in Java? Or all the Option & Result in Rust? throws, throws, throws… unwrap, unwrap, unwrap… (Many) people hate those features

        PHP works against you. And that’s why its bad.

        • Rikudou_Sage@lemmings.world
          link
          fedilink
          arrow-up
          3
          arrow-down
          1
          ·
          1 year ago

          Show us on this doll where php hurt you. Php is great, especially in the later versions. I rarely need to know the position of a substring in a string, most of the time str_starts_with(), str_ends_with() and str_contains() is what I really need.

      • pazukaza@lemmy.ml
        link
        fedilink
        arrow-up
        5
        arrow-down
        1
        ·
        1 year ago

        But if you code like a moron the code should still behave as expected. People who code like this deserve a special place in hell, next to languages that behave like that.

      • bastian_5@sh.itjust.works
        link
        fedilink
        arrow-up
        8
        arrow-down
        5
        ·
        1 year ago

        I say that php breaks math entirely, and is therefore bad. “” == null returns true null == [] returns true “” == [] returns false.

        In more recent versions it gets worse, because it has 0 == “any text” return true, “any text” == true return true, and 1 == true return true So indirectly 1 = 0, and now math is more directly broken.

        • thatwill@lemmy.world
          link
          fedilink
          arrow-up
          13
          arrow-down
          1
          ·
          1 year ago

          If you’re trying to directly compare different variable types in any language without strong typing, you’re going to have edge-case results which you might not expect.

          My “coding like a moron” message still stands. PHP isn’t a strongly typed language and it doesn’t tell you off for trying stupid stuff like comparing a string with an int. Nor do other languages like JavaScript.

          • kimpilled@infosec.pub
            link
            fedilink
            arrow-up
            4
            ·
            1 year ago

            Also using duck typing fails against php is pretty funny when it’s being compared against JAVASCRIPT of all things.

        • thatwill@lemmy.world
          link
          fedilink
          arrow-up
          2
          ·
          1 year ago

          I just tested these out out of curiosity.
          0==“text” returns false in PHP 8.2 as I’d expect.

          The others make sense in the way that php juggles between types. An empty variable can type-juggle to null, but an array can’t be directly compared with a string.

          (Although you wouldn’t really want to compare an array with a string, PHP just treats an array as greater than other variables. So weirdly, ([] > “”) == true.)