• kibiz0r@lemmy.world
    link
    fedilink
    arrow-up
    112
    arrow-down
    3
    ·
    1 year ago

    To be fair: If you are chaining ternary expressions, you deserve to suffer whatever pain the language happens to inflict upon you tenfold.

          • Kryomaani@sopuli.xyz
            link
            fedilink
            English
            arrow-up
            14
            arrow-down
            1
            ·
            1 year ago

            Which is exactly why you shouldn’t be using them in a situation that clearly calls for a switch.

            • Serdan@lemm.ee
              link
              fedilink
              English
              arrow-up
              4
              arrow-down
              1
              ·
              edit-2
              1 year ago

              In the given example I’d probably use a switch / match expression, but ternaries are usually more flexible than switches and I don’t think it’s an issue to write a nested ternary instead of if else statements.

              • lowleveldata@programming.dev
                link
                fedilink
                English
                arrow-up
                4
                arrow-down
                2
                ·
                edit-2
                1 year ago

                ternaries are usually more flexible than switches

                Which is bad for readability because the reader need to manually compute it to see whether it’s doing simple switching or not. Also it adds the question of “Why did the author use a nested ternary instead of a switch? Was it meant to do more but it got left out unintentionally?”

                • Serdan@lemm.ee
                  link
                  fedilink
                  English
                  arrow-up
                  3
                  arrow-down
                  1
                  ·
                  1 year ago

                  Yes, you need to read code to understand it. If else statements can also do the job of a switch, so the exact same argument applies.

      • deaf_fish@lemm.ee
        link
        fedilink
        arrow-up
        1
        arrow-down
        1
        ·
        1 year ago

        There is usually a safer and more readable way to do what you want to do by chaining ternaries in most languages.

          • deaf_fish@lemm.ee
            link
            fedilink
            arrow-up
            1
            arrow-down
            1
            ·
            edit-2
            1 year ago

            Well, if you assume ternary operations work the same in PHP as in c and attempted to write the code demoed by this meme. You would end up with unexpected behavior. Maybe I should have said unexpected behavior instead of unsafe behavior.

            • Serdan@lemm.ee
              link
              fedilink
              arrow-up
              2
              arrow-down
              1
              ·
              1 year ago

              PHP is the only language in existence with a left associative ternary operator. Ignoring PHP, the operator has worked exactly the same way for decades. And even PHP has now fixed the operator.

              I don’t think it’s reasonable to avoid a very commonly supported pattern just because a single badly designed language implemented it wrong.

              • deaf_fish@lemm.ee
                link
                fedilink
                arrow-up
                2
                arrow-down
                1
                ·
                1 year ago

                Okay, even if I give you the unexpected behavior point. The readability problem remains. Switch statements or tables will work just fine and are easier to read.

                To be clear, I am fine with single ternary operations. I think nested ternary operations are harder to read and follow.

                • Serdan@lemm.ee
                  link
                  fedilink
                  arrow-up
                  1
                  ·
                  1 year ago

                  I agree you should use a switch where applicable, but ternaries are the expression equivalent of if-else statements. If I have two conditions and a default, and each branch simply evaluates to a value of the same type, I’ll probably just use a ternary.

  • asceticism@lemmy.fmhy.ml
    link
    fedilink
    arrow-up
    28
    ·
    edit-2
    1 year ago

    This is not valid syntax as of 2020. PHP 8 fixed a lot of issue like this as well as a lot of function and variable type issues.

    Also this was deprecated in PHP 7 (2015).

  • kolorafa@lemmy.world
    link
    fedilink
    English
    arrow-up
    26
    arrow-down
    1
    ·
    1 year ago

    PHP Fatal error: Unparenthesized a ? b : c ? d : e is not supported. Use either (a ? b : c) ? d : e or a ? b : (c ? d : e)

  • finn@lemmy.world
    link
    fedilink
    arrow-up
    25
    arrow-down
    1
    ·
    1 year ago

    Ever wondered about the array_fill function? It can be baffling. Try filling an array with a negative index:

    array_fill(-5, 4, 'test');

    Many languages would throw an error, but PHP? It’s perfectly fine with this and you get an array starting at index -5. It’s like PHP is the Wild West of array indexing!

    • marcos@lemmy.world
      link
      fedilink
      arrow-up
      11
      ·
      1 year ago

      Well, many languages are perfectly ok with negative array indexes.

      But all of those languages are either statically typed ones where you declare the boundings with the array, or php.

      • finn@lemmy.world
        link
        fedilink
        arrow-up
        10
        ·
        1 year ago

        Absolutely, many languages do allow negative indices. The intriguing part about PHP, though, is that its ‘arrays’ are actually ordered maps (or hash tables) under the hood. This structure allows for a broader range of keys, like our negative integers or even strings. It’s a unique design choice that sets PHP apart and allows for some really interesting usage patterns. Not your everyday array, right?

        • Funwayguy@lemmy.world
          link
          fedilink
          arrow-up
          3
          ·
          1 year ago

          I’ve been working with PHP for two years now (not by choice) but I still sometimes forget the weird behaviours these not-arrays cause. Recently I was pushing/popping entries in a queue and it fucked the indexing. I had programmed it like I would any other sane language and it wasn’t until I was stepping through the bug I realised I had forgotten about this.

          I hate PHP for so many more reasons. It baffles me why anyone would think it was a good idea to design it this way. Thankfully my current job involves actively burning it down and preparing for its replacement.

  • Mike@lemmy.ml
    link
    fedilink
    arrow-up
    34
    arrow-down
    12
    ·
    1 year ago

    Hating on php is one of the reasons i left reddit. This is just people who don’t use php hating php for some reason. You can do dumb examples like this for any language. Low effort and funny for children.

    • deaf_fish@lemm.ee
      link
      fedilink
      arrow-up
      7
      arrow-down
      1
      ·
      1 year ago

      Your feelings are valid. I wonder though, would you put up this level of defense for posts making fun of arbitrary parts of non PHP languages?

      You are not your favorite language. And I find most criticisms of most languages to be very valid. I don’t think the intent of OP is to insult all PHP programmers. It’s okay to like a language that has problems. All languages do.

      • Doug [he/him]@midwest.social
        link
        fedilink
        arrow-up
        1
        ·
        1 year ago

        I’d wager prevalence is part of their problem. Jokes get tired after a while, but that doesn’t always mean they stop.

        PHP, like any language, has its problems, but it seems to get poked at a lot more often. But making the same joke over and over has been a problem long before reddit was a thing.

      • Mike@lemmy.ml
        link
        fedilink
        arrow-up
        3
        arrow-down
        3
        ·
        1 year ago

        PHP gets a totally disproportionate share of hate and that is the problem. Children like to dunk on PHP and a group mentality pushes it even more.

        • deaf_fish@lemm.ee
          link
          fedilink
          arrow-up
          1
          arrow-down
          1
          ·
          1 year ago

          Maybe it does. Maybe it doesn’t. This is the first post I have seen pointing out a flaw in PHP on Reddit or Lemmy. If you ask me JavaScript gets it the worst out of all of the languages. I don’t see those guys whining.

          What I will say is that the majority of PHP developers I interact with on this post has led me to believe that there is a number of PHP developers that take things way to personally that they really shouldn’t. Seriously you guys aren’t doing your language any favors. No one’s going to want to join the whine club.

    • BinarySystem@lemmy.fmhy.ml
      link
      fedilink
      arrow-up
      2
      arrow-down
      1
      ·
      1 year ago

      I took this more as a light-hearted poke at a silly edge case. As someone who used to build static analysis software for various languages, including PHP. This gets a chuckle out of me as it takes me back to having to deal with these exact types of edge cases.

      • Mike@lemmy.ml
        link
        fedilink
        arrow-up
        2
        arrow-down
        2
        ·
        1 year ago

        The title of this post is calling php a “meme” did you read that part? Then it just uses some stupid ternary example no one does and that other languages exhibit so what is the point other than purely hating on php?

  • dot20@lemmy.world
    link
    fedilink
    arrow-up
    18
    ·
    1 year ago

    Ah, I understand now. The expression is evaluated like this:

    • $a == 1 ? "one" : $a == 2 ? "two" : $a == 3 ? "three" : "other"
    • $a == 2 ? "two" : $a == 3 ? "three" : "other"
    • "two" ? "three" : "other"
    • "three"
      • sanguinet@lemmy.ca
        link
        fedilink
        arrow-up
        22
        ·
        1 year ago

        It’s cause PHP associates the if-then-else pair only with its immediate “else” option, not with the entirety of the line.

        Let’s go by parts.

        $a == 1 ? "one" : $a == 2 ? "two" : $a == 3 ? "three" : "other"

        Is $a equal to 1? If so, we’re “set” to the value on the left, which is "one", if not then we’re set to the value on the right, which is $a == 2. $a is not equal to 1, so we’re set to the right value, $a == 2.

        This replaces the relevant part, $a == 1 ? "one" : $a == 2, with $a == 2. So we’re left with:

        $a == 2 ? "two" : $a == 3 ? "three" : "other"

        Next, is $a equal to 2? If so, we’re set to "two", if not we’re set to $a == 3. The comparison is true, so we’re set to the value on the left, "two". The relevant part here is $a == 2 ? "two" : $a == 3 only, so it replaces it with "two" cause again, PHP is only associating with its immediate pair. So now we’re left with:

        "two" ? "three" : "other"

        Finally, is "two" truthy? If so, we’re set to "three", if not we’re set to "other". Since "two" is truthy we’re then left with "three".

        It’s super confusing for sure.

  • xedrak@kbin.social
    link
    fedilink
    arrow-up
    2
    ·
    1 year ago

    I get hating on PHP is a meme, and the language certainly has faults, but I feel like it’s no more arbitrary than how JavaScript behaves. And just like JavaScript, if you follow modern standards and use a modern version, it’s a much better experience. The language is only as good as the programmer.

    • CanadaPlus@lemmy.sdf.org
      link
      fedilink
      arrow-up
      1
      arrow-down
      1
      ·
      edit-2
      1 year ago

      but I feel like it’s no more arbitrary than how JavaScript behaves

      This is not the flex you think it is.

  • Lemmypy@feddit.nl
    link
    fedilink
    arrow-up
    1
    ·
    1 year ago

    Finally got it…

    $a == 1 ? "one" : ( ( $a == 2 ? "two" : $a == 3 ) ? "three" : "other" )