Butt ugly

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Rik G.

    Butt ugly

    Coming from an Assembler/C/C++/C# background I got to say this is butt ugly:

    <?php
    echo "2 + 2 = " . 2+2; // This will print 4
    echo "2 + 2 = " , 2+2; // This will print 2 + 2 = 4
    echo "test " . 2+2; // This will print 2
    ?>

    R.


  • Jerry Stuckle

    #2
    Re: Butt ugly

    Rik G. wrote:[color=blue]
    > Coming from an Assembler/C/C++/C# background I got to say this is butt ugly:
    >
    > <?php
    > echo "2 + 2 = " . 2+2; // This will print 4
    > echo "2 + 2 = " , 2+2; // This will print 2 + 2 = 4
    > echo "test " . 2+2; // This will print 2
    > ?>
    >
    > R.
    >
    >[/color]

    Not if you understand precedence. Remember, '.' has a high precedence. The
    following all work:


    <?php
    echo "2 + 2 = " . (2+2); // This will print 2 + 2 = 4
    echo "2 + 2 = " , (2+2); // This will print 2 + 2 = 4
    echo "test " . (2+2); // This will print test 4
    ?>


    --
    =============== ===
    Remove the "x" from my email address
    Jerry Stuckle
    JDS Computer Training Corp.
    jstucklex@attgl obal.net
    =============== ===

    Comment

    • Rik G.

      #3
      Re: Butt ugly

      "Jerry Stuckle" <jstucklex@attg lobal.net> wrote in message
      news:pbqdnZunEt tRouXZRVn-gg@comcast.com. ..[color=blue]
      > Rik G. wrote:[color=green]
      > > Coming from an Assembler/C/C++/C# background I got to say this is butt[/color][/color]
      ugly:[color=blue][color=green]
      > >
      > > <?php
      > > echo "2 + 2 = " . 2+2; // This will print 4
      > > echo "2 + 2 = " , 2+2; // This will print 2 + 2 = 4
      > > echo "test " . 2+2; // This will print 2
      > > ?>
      > >
      > > R.
      > >
      > >[/color]
      >
      > Not if you understand precedence. Remember, '.' has a high precedence.[/color]
      The[color=blue]
      > following all work:
      >
      >
      > <?php
      > echo "2 + 2 = " . (2+2); // This will print 2 + 2 = 4
      > echo "2 + 2 = " , (2+2); // This will print 2 + 2 = 4
      > echo "test " . (2+2); // This will print test 4
      > ?>[/color]

      OK, thanks, that looks better but still: why does it eat the strings "2 + 2
      = " and "test " in case 1 and 3?

      And here's for something even but uglier:

      echo "2 + 2 = " . 2+3; // This will print 5
      echo "2 + 2 = " , 2+3; // This will print 2 + 2 = 5
      echo "test " . 2+3; // This will print 3


      Comment

      • Rik

        #4
        Re: Butt ugly

        Jerry Stuckle wrote:[color=blue]
        > Rik G. wrote:[color=green]
        >> Coming from an Assembler/C/C++/C# background I got to say this is
        >> butt ugly:
        >>
        >> <?php
        >> echo "2 + 2 = " . 2+2; // This will print 4
        >> echo "2 + 2 = " , 2+2; // This will print 2 + 2 = 4
        >> echo "test " . 2+2; // This will print 2[/color]
        >
        > Not if you understand precedence. Remember, '.' has a high
        > precedence. The following all work:
        >
        >
        > <?php
        > echo "2 + 2 = " . (2+2); // This will print 2 + 2 = 4
        > echo "2 + 2 = " , (2+2); // This will print 2 + 2 = 4
        > echo "test " . (2+2); // This will print test 4[/color]

        Not only precedence, type juggling also plays a major part here.
        Detailed:
        [color=blue][color=green]
        >> echo "2 + 2 = " . 2+2; // This will print 4[/color][/color]

        1. The string "2 + 2 = " becomes "2 + 2 = 2" (indeed precedence).
        2. Trying to add a number to this string casts it to 2 (the first number),
        and adds 2, so gives 4.
        [color=blue][color=green]
        >> echo "2 + 2 = " , 2+2; // This will print 2 + 2 = 4[/color][/color]

        The string "2 + 2 = " gets echoed seperately from the integer resulting from
        adding 2+2
        [color=blue][color=green]
        >> echo "test " . 2+2; // This will print 2[/color][/color]

        1. The string "test " becomes "test 2".
        2. Trying to add the number 2 to "test 2" casts the string to an integer
        (0), and adding 2 gives indeed 2

        Variable variables, without strict type, are a blessing in some cases, in
        others it's immensely irritating. You just have to keep an eye on it :-).

        Reading material:


        Table 15-1

        When in doubt, cast the piece of code to a certain type by (type) (e.g.
        (bool), (string), (int) etc.).

        Grtz
        --
        Rik Wasmus


        Comment

        • Rik G.

          #5
          Re: Butt ugly


          "Rik" <luiheidsgoeroe @hotmail.com> wrote in message
          news:de52b$4478 46ce$8259c69c$2 2611@news1.tude lft.nl...

          [Snip]
          [color=blue]
          > Not only precedence, type juggling also plays a major part here.
          > Detailed:
          >[color=green][color=darkred]
          > >> echo "2 + 2 = " . 2+2; // This will print 4[/color][/color]
          >
          > 1. The string "2 + 2 = " becomes "2 + 2 = 2" (indeed precedence).
          > 2. Trying to add a number to this string casts it to 2 (the first number),
          > and adds 2, so gives 4.
          >[color=green][color=darkred]
          > >> echo "2 + 2 = " , 2+2; // This will print 2 + 2 = 4[/color][/color]
          >
          > The string "2 + 2 = " gets echoed seperately from the integer resulting[/color]
          from[color=blue]
          > adding 2+2
          >[color=green][color=darkred]
          > >> echo "test " . 2+2; // This will print 2[/color][/color]
          >
          > 1. The string "test " becomes "test 2".
          > 2. Trying to add the number 2 to "test 2" casts the string to an integer
          > (0), and adding 2 gives indeed 2
          >
          > Variable variables, without strict type, are a blessing in some cases, in
          > others it's immensely irritating. You just have to keep an eye on it :-).[/color]

          OK, it's clear now (but still butt ugly).
          Thanks for the explanation.

          R.


          Comment

          • Rik

            #6
            Re: Butt ugly

            Rik G. wrote:[color=blue]
            > OK, thanks, that looks better but still: why does it eat the strings
            > "2 + 2 = " and "test " in case 1 and 3?[/color]

            See my other post.
            [color=blue]
            > And here's for something even but uglier:
            >
            > echo "2 + 2 = " . 2+3; // This will print 5[/color]

            1. String "2 + 2 = ".
            2. String "2 + 2 = 2".
            3. Gets cast to an integer because of +, which results in the first number:
            integer "2".
            4. Integer 2 + Integer 3 = 5
            [color=blue]
            > echo "2 + 2 = " , 2+3; // This will print 2 + 2 = 5[/color]

            1. String "2 + 2 = " get's echoed seperately.
            2. Calculation 2+3 is performed.
            3. Integer 5 is echoed seperately (the , operator is way, way down the
            precedence list).
            [color=blue]
            > echo "test " . 2+3; // This will print 3[/color]

            1. String "test ".
            2. String "test 2".
            3. String gets cast to an integer, the first character is not a number or
            '-' followed by a number, so gets cast to integer 0.
            4. 0+3 = 3

            Grtz,
            --
            Rik Wasmus


            Comment

            • Iván Sánchez Ortega

              #7
              Re: Butt ugly

              -----BEGIN PGP SIGNED MESSAGE-----
              Hash: SHA1

              Rik G. wrote:
              [color=blue]
              > Coming from an Assembler/C/C++/C# background I got to say this is butt
              > ugly:[/color]

              It's called "operator precedente and automatic type casting":
              [color=blue]
              > <?php
              > echo "2 + 2 = " . 2+2; // This will print 4[/color]

              Explicit operator precedence:

              echo ("2 + 2 = " . 2) + 2;
              echo ("2 + 2 = 2") + 2;

              Wait, there is a sum operator over there, both operands must be
              scalar-typed:

              echo ( (int) "2 + 2 =2") + 2;

              And, if you RTFM about casting a string to integer:

              echo ( 2 ) + 2;


              - --
              - ----------------------------------
              Iván Sánchez Ortega -i-punto-sanchez--arroba-mirame-punto-net


              Proudly running Debian Linux with 2.6.15-1-686 kernel, KDE3.5.0, and PHP
              5.1.2-1+b1 generating this signature.
              Uptime: 14:47:44 up 30 min, 1 user, load average: 0.88, 1.55, 1.32

              -----BEGIN PGP SIGNATURE-----
              Version: GnuPG v1.4.3 (GNU/Linux)

              iD8DBQFEeEsj3jc Q2mg3Pc8RAncbAK CAGbEAAgvBM/kyWYPN09nsptooz gCfZLMF
              nGAewVd/7NJZGPbHLwUSWhQ =
              =B+Vj
              -----END PGP SIGNATURE-----

              Comment

              • Bent Stigsen

                #8
                Re: Butt ugly

                Rik G. wrote:[color=blue]
                > "Rik" <luiheidsgoeroe @hotmail.com> wrote in message
                > news:de52b$4478 46ce$8259c69c$2 2611@news1.tude lft.nl...
                >[/color]
                [snip][color=blue][color=green][color=darkred]
                >>>> echo "test " . 2+2; // This will print 2[/color]
                >> 1. The string "test " becomes "test 2".
                >> 2. Trying to add the number 2 to "test 2" casts the string to an integer
                >> (0), and adding 2 gives indeed 2
                >>
                >> Variable variables, without strict type, are a blessing in some cases, in
                >> others it's immensely irritating. You just have to keep an eye on it :-).[/color]
                >
                > OK, it's clear now (but still butt ugly).[/color]

                It was your choice to write it like that. If you don't want the
                freedom to write something so badly, then don't use a language that
                lets you do it.

                It's like that doctor/patient-joke:

                [Patient]: doctor, it hurts when I do this.
                [Doctor] : then don't do that.


                /Bent

                [snip]

                Comment

                • Chung Leong

                  #9
                  Re: Butt ugly

                  Jerry Stuckle wrote:[color=blue]
                  > Not if you understand precedence. Remember, '.' has a high precedence. The
                  > following all work:[/color]

                  The . operator has the same precedence as + and -, the wisdom of which
                  is debatable. I don't think it's intuitive that

                  echo 2+2 . ' = 2 + 2';

                  prints out '4 = 2 + 2' while

                  echo '2 + 2 = ' . 2+2;

                  prints out 4.

                  Comment

                  • Rik

                    #10
                    Re: Butt ugly

                    Chung Leong wrote:[color=blue]
                    > Jerry Stuckle wrote:[color=green]
                    >> Not if you understand precedence. Remember, '.' has a high
                    >> precedence. The following all work:[/color]
                    >
                    > The . operator has the same precedence as + and -, the wisdom of which
                    > is debatable. I don't think it's intuitive that
                    > echo 2+2 . ' = 2 + 2';
                    > prints out '4 = 2 + 2' while
                    > echo '2 + 2 = ' . 2+2;
                    > prints out 4.[/color]

                    The only thing WHY this isn't intuitive is because of the irregular use of
                    spaces. Why surround the . by spaces, and not the +? You're only making
                    thinks blurry for yourself.

                    Typing:
                    echo 2 + 2 . ' = 2 + 2';
                    echo '2 + 2 = ' . 2 + 2;
                    ... is a lot clearer.

                    Further, it's like any Western language: we read from left to right. If
                    precedence doesn't sort it out, it's processed as you write. Type juggle
                    necessary for the last operations stands. It seems very logical to me. What
                    can make it confusing, is the fact that the string contains characters that
                    could be operators. That could throw you of, just because you expect
                    something that you know since you were 6.

                    Let's say $text = "2 + 2 = " or " = 2 + 2"

                    echo 2 + 2 . $text;
                    echo $text . 2 + 2;

                    It just makes sense to me....

                    Grtz,
                    --
                    Rik Wasmus


                    Comment

                    • Chung Leong

                      #11
                      Re: Butt ugly

                      Rik wrote:[color=blue]
                      > Further, it's like any Western language: we read from left to right. If
                      > precedence doesn't sort it out, it's processed as you write. Type juggle
                      > necessary for the last operations stands. It seems very logical to me. What
                      > can make it confusing, is the fact that the string contains characters that
                      > could be operators. That could throw you of, just because you expect
                      > something that you know since you were 6.[/color]

                      That's sort of my point. Precedence should determine the outcome
                      instead the order of the operators. Giving operators that are
                      fundamentally different the same precedence is confusing, because
                      people tend to group operations they consider similiar together in
                      their mind. Can you imagine how confusing it would be if + and == has
                      the same precdence? Quickly, what's the result of the following:

                      2 + 2 == 1 + 3
                      [color=blue]
                      > Let's say $text = "2 + 2 = " or " = 2 + 2"
                      >
                      > echo 2 + 2 . $text;
                      > echo $text . 2 + 2;
                      >
                      > It just makes sense to me....[/color]

                      Well, it didn't make sense to the other Rik ;-)

                      Comment

                      • Norman Peelman

                        #12
                        Re: Butt ugly

                        "Chung Leong" <chernyshevsky@ hotmail.com> wrote in message
                        news:1148755585 .431248.282160@ j55g2000cwa.goo glegroups.com.. .[color=blue]
                        > Rik wrote:[color=green]
                        > > Further, it's like any Western language: we read from left to right. If
                        > > precedence doesn't sort it out, it's processed as you write. Type juggle
                        > > necessary for the last operations stands. It seems very logical to me.[/color][/color]
                        What[color=blue][color=green]
                        > > can make it confusing, is the fact that the string contains characters[/color][/color]
                        that[color=blue][color=green]
                        > > could be operators. That could throw you of, just because you expect
                        > > something that you know since you were 6.[/color]
                        >
                        > That's sort of my point. Precedence should determine the outcome
                        > instead the order of the operators. Giving operators that are
                        > fundamentally different the same precedence is confusing, because
                        > people tend to group operations they consider similiar together in
                        > their mind. Can you imagine how confusing it would be if + and == has
                        > the same precdence? Quickly, what's the result of the following:
                        >
                        > 2 + 2 == 1 + 3
                        >[color=green]
                        > > Let's say $text = "2 + 2 = " or " = 2 + 2"
                        > >
                        > > echo 2 + 2 . $text;
                        > > echo $text . 2 + 2;
                        > >
                        > > It just makes sense to me....[/color]
                        >
                        > Well, it didn't make sense to the other Rik ;-)
                        >[/color]

                        It makes sense because as soon as you try to ADD a number to a 'string'
                        (or a 'string' to a 'string'), the strings are lost (counted as zero):

                        echo 'A + B = ' . 'A' + 'B';

                        will output zero (0)
                        'A + B = ' . 0 + 0 = 0
                        0 . 0 = 0
                        0 = 0

                        echo 'A + B = ' . 'A' + 'B' + 100;

                        will output 100 (0 + 100)
                        'A + B = ' . 0 + 0 + 100 = 100
                        0 . 0 + 100 = 100
                        0 + 100 = 100
                        100 = 100

                        echo 'A + B = ' . 100;

                        will output A + B = 100
                        as the number is converted to string for concatination NOT addition. It's
                        not really about precidence at all as no math is done within quoted strings
                        (literals).

                        ....but, you can:

                        echo 'A + B = ' . ('100' + '200') --> A + B = 300
                        echo 'A + B = ' . ('100A' + '200B') --> A + B = 300

                        but not...

                        echo 'A + B = ' . ('A100' + '200B') --> A + B = 200
                        'A100' is converted to zero (0)

                        Norm


                        Comment

                        • Oli Filth

                          #13
                          Re: Butt ugly

                          Norman Peelman said the following on 28/05/2006 03:45:[color=blue]
                          > "Chung Leong" <chernyshevsky@ hotmail.com> wrote in message
                          > news:1148755585 .431248.282160@ j55g2000cwa.goo glegroups.com.. .[color=green]
                          >> Rik wrote:[color=darkred]
                          >>> Let's say $text = "2 + 2 = " or " = 2 + 2"
                          >>>
                          >>> echo 2 + 2 . $text;
                          >>> echo $text . 2 + 2;
                          >>>
                          >>> It just makes sense to me....[/color]
                          >> Well, it didn't make sense to the other Rik ;-)
                          >>[/color]
                          > It makes sense because as soon as you try to ADD a number to a 'string'
                          > (or a 'string' to a 'string'), the strings are lost (counted as zero):
                          >
                          > echo 'A + B = ' . 'A' + 'B';
                          >
                          > will output zero (0)
                          > 'A + B = ' . 0 + 0 = 0
                          > 0 . 0 = 0
                          > 0 = 0[/color]

                          Actually, that's doing:
                          'A + B = ' . 'A' + 'B'
                          'A + B = A' + 'B'
                          0 + 0
                          0

                          (the . is evaluated first, due to left associativity.)

                          [color=blue]
                          > echo 'A + B = ' . 'A' + 'B' + 100;
                          >
                          > will output 100 (0 + 100)
                          > 'A + B = ' . 0 + 0 + 100 = 100
                          > 0 . 0 + 100 = 100
                          > 0 + 100 = 100
                          > 100 = 100[/color]

                          Similarly for this one.

                          [color=blue]
                          > It's
                          > not really about precidence at all as no math is done within quoted strings
                          > (literals).[/color]

                          It's *all* about precedence (and associativity, indirectly), and that's
                          the point Chung was making.

                          The fact that the examples all contain things like "A + B = " is
                          irrelevant, and just confusing things...



                          --
                          Oli

                          Comment

                          • Chung Leong

                            #14
                            Re: Butt ugly

                            Norman Peelman wrote:[color=blue]
                            > It makes sense because as soon as you try to ADD a number to a 'string'
                            > (or a 'string' to a 'string'), the strings are lost (counted as zero):[/color]

                            You might as well argue that 1 + 2 * 3 should equal 9. "As soon as you
                            add 2 to 1, you get 3..."

                            Addition and multiplication are communtative operations. 1 + 6 = 6 + 1
                            and 2 * 3 = 3 * 2. Thanks to precedence, they maintains their
                            properties in more complex expression: 1 + 2 * 3 = 1 + 3 * 2 = 3 * 2 +
                            1.

                            In PHP, because . and + have the same precedence, addition is no longer
                            communtative: "0." . 7 + 5 yields a different result from "0." . 5 +
                            7. So if you will, please explain how violating elementary mathematical
                            principles makes sense.

                            Comment

                            • the DtTvB

                              #15
                              Re: Butt ugly

                              I use ( and )
                              And yeah, it's confusing when I use PHP with JavaScript.

                              Comment

                              Working...