difference between ' and "

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

    #1

    difference between ' and "

    Today I spent a couple of hours searching for a bug, until I finally
    found out that the problems was the following:

    Correct: $path_upload = "$path/$file";

    Instead of:

    Wrong: $path_upload = '$path/$file';

    Note a difference in quotation marks. Since I have been programming in
    PHP I found out that there is very little difference between ' and ",
    but is there somebody who can explain me, when to use ' and when to
    use ". It would be really great if there is somebody to explain this
    to me!

    Thanks,
    Jochem
  • Pedro Graca

    #2
    Re: difference between ' and "

    Jochem wrote:[color=blue]
    > Since I have been programming in
    > PHP I found out that there is very little difference between ' and ",
    > but is there somebody who can explain me, when to use ' and when to
    > use ". It would be really great if there is somebody to explain this
    > to me![/color]

    *NEVER* use ", unless you _need_ them

    I only need " for two things:
    embedding \n, \t and similar
    simplify SQL queries, but I don't have to like it :)

    $name1 = 'Pedro Graca'; // ok
    $name2 = "Pedro Graca"; // forces PHP to do extra work
    $name3 = $name1; // ok
    $name4 = "$name2"; // UGH! *TRIPLE* UGH!!!!
    // people who do this should be whipped


    echo 'Your name is: ', $name, "<br/>\n";

    $sql = "insert into table values($new_id, '$new_name')";


    // for comparison here is the echo with only " (simpler, but not needed)
    // echo "Your name is: $name<br/>\n";

    // and the $sql with only ' (more awkward)
    // $sql = 'insert into table values(' . $new_id . ', \'' . $new_name . '\')';
    --
    --= my mail box only accepts =--
    --= Content-Type: text/plain =--
    --= Size below 10001 bytes =--

    Comment

    • Tim Van Wassenhove

      #3
      Re: difference between ' and &quot;

      On 2003-12-30, Jochem <jdonkers@opera mail.com> wrote:[color=blue]
      > Today I spent a couple of hours searching for a bug, until I finally
      > found out that the problems was the following:
      >
      > Correct: $path_upload = "$path/$file";
      >
      > Instead of:
      >
      > Wrong: $path_upload = '$path/$file';
      >
      > Note a difference in quotation marks. Since I have been programming in
      > PHP I found out that there is very little difference between ' and ",
      > but is there somebody who can explain me, when to use ' and when to
      > use ". It would be really great if there is somebody to explain this
      > to me![/color]




      --
      verum ipsum factum

      Comment

      • Tim Van Wassenhove

        #4
        Re: difference between ' and &quot;

        On 2003-12-30, Pedro Graca <hexkid@hotpop. com> wrote:
        [color=blue]
        > $name4 = "$name2"; // UGH! *TRIPLE* UGH!!!!
        > // people who do this should be whipped[/color]

        I think people only come up with this one if they have a Bash background
        ;)


        [color=blue]
        > // for comparison here is the echo with only " (simpler, but not needed)
        > // echo "Your name is: $name<br/>\n";[/color]

        In this case you would need: "Your name is: {$name}<br>\n";

        --
        verum ipsum factum

        Comment

        • Drebbel

          #5
          Re: difference between ' and &quot;

          Jochem wrote:[color=blue]
          > Today I spent a couple of hours searching for a bug, until I finally
          > found out that the problems was the following:
          >
          > Correct: $path_upload = "$path/$file";
          >
          > Instead of:
          >
          > Wrong: $path_upload = '$path/$file';
          >
          > Note a difference in quotation marks. Since I have been programming in
          > PHP I found out that there is very little difference between ' and ",
          > but is there somebody who can explain me, when to use ' and when to
          > use ". It would be really great if there is somebody to explain this
          > to me![/color]

          Main difference to me is that when using "" php is looking for variables
          between the "".

          - D -


          Comment

          • Agelmar

            #6
            Re: difference between ' and &quot;

            Tim Van Wassenhove wrote:[color=blue]
            > On 2003-12-30, Pedro Graca <hexkid@hotpop. com> wrote:
            >[color=green]
            >> $name4 = "$name2"; // UGH! *TRIPLE* UGH!!!!
            >> // people who do this should be whipped[/color]
            >
            > I think people only come up with this one if they have a Bash
            > background ;)
            >
            >
            >[color=green]
            >> // for comparison here is the echo with only " (simpler, but not
            >> needed) // echo "Your name is: $name<br/>\n";[/color]
            >
            > In this case you would need: "Your name is: {$name}<br>\n";[/color]

            Actually no,
            echo "Some variable is $somevar dude!"; is perfectly fine. You only need { }
            for arrays inside a string, e.g.
            echo "Some variable is {$somevar['name']} dude!";
            although inside of a string you can leave off the quotes on the key and have
            echo "Some variable is $somevar[name] dude!";
            While the last is proper according to the manual, I find it poor form and
            prefer "somevar is {$something['blah']}".


            Comment

            • Tim Van Wassenhove

              #7
              Re: difference between ' and &quot;

              On 2003-12-30, Agelmar <ifetteNOSPAM@c omcast.net> wrote:[color=blue]
              > Tim Van Wassenhove wrote:[color=green]
              >> On 2003-12-30, Pedro Graca <hexkid@hotpop. com> wrote:
              >>[color=darkred]
              >>> $name4 = "$name2"; // UGH! *TRIPLE* UGH!!!!
              >>> // people who do this should be whipped[/color]
              >>
              >> I think people only come up with this one if they have a Bash
              >> background ;)
              >>
              >>
              >>[color=darkred]
              >>> // for comparison here is the echo with only " (simpler, but not
              >>> needed) // echo "Your name is: $name<br/>\n";[/color]
              >>
              >> In this case you would need: "Your name is: {$name}<br>\n";[/color]
              >
              > Actually no,
              > echo "Some variable is $somevar dude!"; is perfectly fine. You only need { }
              > for arrays inside a string, e.g.[/color]

              Actually yes, the OP wrote: echo "Your name is: $name<br/>\n";
              No space between the variable and <br>.
              [color=blue]
              > echo "Some variable is {$somevar['name']} dude!";
              > although inside of a string you can leave off the quotes on the key and have
              > echo "Some variable is $somevar[name] dude!";
              > While the last is proper according to the manual, I find it poor form and
              > prefer "somevar is {$something['blah']}".[/color]

              Not using quotes is not only poor, it is also slower.


              --
              verum ipsum factum

              Comment

              • John Dunlop

                #8
                Re: difference between ' and &quot;

                Tim Van Wassenhove wrote:
                [color=blue]
                > Actually yes,[/color]

                Actually no. SCNR. ;-)
                [color=blue]
                > the OP wrote: echo "Your name is: $name<br/>\n";
                > No space between the variable and <br>.[/color]

                A LESS-THAN SIGN, just like a SPACE, isn't a valid variable name
                character, so the variable $name will be recognised.



                --
                Jock

                Comment

                • Five Cats

                  #9
                  Re: difference between ' and &quot;

                  In message <bsrv9f$pkjk$1@ ID-203069.news.uni-berlin.de>, Pedro Graca
                  <hexkid@hotpop. com> writes
                  <snip>[color=blue]
                  >
                  >$name1 = 'Pedro Graca'; // ok
                  >$name2 = "Pedro Graca"; // forces PHP to do extra work[/color]
                  <snip>

                  Could you explain why the double quotes make PHP do more work than the
                  single quotes, please?

                  --
                  Five Cats
                  Email to: cats_spam at uk2 dot net

                  Comment

                  • Andy Fraser

                    #10
                    Re: difference between ' and &quot;

                    Five Cats uttered the immortal words:
                    [color=blue]
                    > <snip>[color=green]
                    >>
                    >>$name1 = 'Pedro Graca'; // ok
                    >>$name2 = "Pedro Graca"; // forces PHP to do extra work[/color]
                    > <snip>
                    >
                    > Could you explain why the double quotes make PHP do more work than the
                    > single quotes, please?[/color]

                    Click the link Tim posted in his first post to this thread.

                    --
                    Andy.

                    Comment

                    • SwissCheese

                      #11
                      Re: difference between ' and &quot;

                      "Five Cats" <cats_spam@[127.0.0.1]> wrote in message
                      news:WvzZFbF4FF 9$EwdB@[127.0.0.1]...[color=blue]
                      > In message <bsrv9f$pkjk$1@ ID-203069.news.uni-berlin.de>, Pedro Graca
                      > <hexkid@hotpop. com> writes
                      > <snip>[color=green]
                      > >
                      > >$name1 = 'Pedro Graca'; // ok
                      > >$name2 = "Pedro Graca"; // forces PHP to do extra work[/color]
                      > <snip>
                      >
                      > Could you explain why the double quotes make PHP do more work than the
                      > single quotes, please?
                      >
                      > --
                      > Five Cats
                      > Email to: cats_spam at uk2 dot net
                      >[/color]

                      Bottom line: PHP parses anything in " " looking for variables to replace
                      with values, anything in ' ' it does not.




                      Comment

                      Working...