When to use single or double quotes

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

    When to use single or double quotes

    Hi,

    I am still confused as when to use single or double quotes.

    This works:

    echo "<td>" . $row[0] . "</td>";

    and this does not

    echo "<td bgcolor="#FFFFF F"><font face="Arial" size="1">" . $row[0] .
    "</font></td>";

    Regards
    Dynamo

  • David Zawislak

    #2
    Re: When to use single or double quotes

    Dynamo wrote:[color=blue]
    > Hi,
    >
    > I am still confused as when to use single or double quotes.
    >
    > This works:
    >
    > echo "<td>" . $row[0] . "</td>";
    >
    > and this does not
    >
    > echo "<td bgcolor="#FFFFF F"><font face="Arial" size="1">" . $row[0] .
    > "</font></td>";[/color]
    Try
    echo "<td bgcolor=\"#FFFF FF\"><font face=\"Arial\" size=\"1\">" . row[0]

    When using the same type of quotes in a string containing the same quote
    type, you need to escape the characters with a backslash \

    or

    echo '<td bgcolor="#FFFFF F"><font face="Arial" size="1">' . $row[0]



    Comment

    • David Zawislak

      #3
      Re: When to use single or double quotes

      David Zawislak wrote:
      [color=blue]
      > Dynamo wrote:
      >[color=green]
      >> Hi,
      >>
      >> I am still confused as when to use single or double quotes.
      >>
      >> This works:
      >>
      >> echo "<td>" . $row[0] . "</td>";
      >>
      >> and this does not
      >>
      >> echo "<td bgcolor="#FFFFF F"><font face="Arial" size="1">" . $row[0] .
      >> "</font></td>";[/color]
      >
      > Try
      > echo "<td bgcolor=\"#FFFF FF\"><font face=\"Arial\" size=\"1\">" . row[0]
      >
      > When using the same type of quotes in a string containing the same quote
      > type, you need to escape the characters with a backslash \
      >
      > or
      >
      > echo '<td bgcolor="#FFFFF F"><font face="Arial" size="1">' . $row[0][/color]

      Also single quotes don't parse variables in the quoted strings nor
      escape sequences.

      To place variables into your string to be parse, use double quotes.

      $x = 5;
      echo '$x' . "= $x \n";

      Comment

      • JaNE

        #4
        Re: When to use single or double quotes

        Dynamo <Dynamo_member@ newsguy.com> wrote:
        [color=blue]
        > Hi,
        >
        > I am still confused as when to use single or double quotes.
        >
        > This works:
        > echo "<td>" . $row[0] . "</td>";[/color]

        I'm also relatively new in php, but have learned that...
        1st double qote start your html code, 2nd ends it.[color=blue]
        >
        > and this does not
        >
        > echo "<td bgcolor="#FFFFF F"><font face="Arial" size="1">" . $row[0] .
        > "</font></td>";
        >[/color]

        here, you are printing in html:
        <td bgcolor=
        and then you are telling to php:
        #FFFFFF and that php doesn't understand

        you have to use backslash so php will know this isn't end of your "html
        part", rather is a "part of html":

        echo "<td bgcolor=\"#FFFF FF\"><font face=\"Arial\" size=\"1">" . $row[0]
        .. "</font></td>";

        btw, it is much better to learn and use css instead of placing those
        font tags in html.
        also, on the end (to have clearer html code) use \n at the and of line:
        ....</font></td>\n";

        and, I dont think you have to break your html line to put your variable
        in... this should work to:

        echo "<td bgcolor=\"#FFFF FF\"><font face=\"Arial\"
        size=\"1">$row[0]</font></td>\n";

        ....and the best:
        <td class=\"arial1\ ">$row[0]</td>\n";

        (however, as I'm not an expert, we will have to vait for someone
        cleverer to tell if that [0] needs to be in some quotes or not...)


        --
        Jan ko?
        fotografija = zapisano svjetlom | fotozine = foto-e-zin

        --

        Comment

        • Pedro Graca

          #5
          Re: When to use single or double quotes

          Dynamo wrote:[color=blue]
          > I am still confused as when to use single or double quotes.[/color]
          [color=blue]
          > echo "<td bgcolor="#FFFFF F"><font face="Arial" size="1">" . $row[0] .[/color]
          ^^^^^^^^^ ^^^^^^^ ^^^
          Quotes within quotes.



          Either you 'escape' them or use single quotes.

          Read http://www.php.net/manual/en/language.types.string.php
          and try these examples.

          echo "He said "WOW" when he saw it.";
          echo "He said 'WOW' when he saw it.";
          echo "He said \"WOW\" when he saw it.";
          echo "He said \'WOW\' when he saw it.";

          /* or with sinle quotes */
          echo 'He said "WOW" when he saw it.';
          echo 'He said 'WOW' when he saw it.';
          echo 'He said \"WOW\" when he saw it.';
          echo 'He said \'WOW\' when he saw it.';

          /* or with embedded variables */
          $x = 'foobar';
          echo "x is $x";
          echo 'x is $x';

          --
          Mail sent to my "From:" address is publicly readable at http://www.dodgeit.com/
          == ** ## !! !! ## ** ==
          TEXT-ONLY mail to the complete "Reply-To:" address ("My Name" <my@address>) may
          bypass the spam filter. I will answer all pertinent mails from a valid address.

          Comment

          • Geoff Berrow

            #6
            Re: When to use single or double quotes

            I noticed that Message-ID: <1gngd7s.1s1lvs c1gf562kN%not_m e@mail.dot>
            from JaNE contained the following:
            [color=blue]
            >...and the best:
            ><td class=\"arial1\ ">$row[0]</td>\n";[/color]

            Mmm, not quite. The class should ideally unambiguously describe the
            section of the document e.g class="product_ table_item"

            --
            Geoff Berrow (put thecat out to email)
            It's only Usenet, no one dies.
            My opinions, not the committee's, mine.
            Simple RFDs http://www.ckdog.co.uk/rfdmaker/

            Comment

            • JaNE

              #7
              Re: When to use single or double quotes

              Geoff Berrow <blthecat@ckdog .co.uk> wrote:
              [color=blue]
              > I noticed that Message-ID: <1gngd7s.1s1lvs c1gf562kN%not_m e@mail.dot>
              > from JaNE contained the following:
              >[color=green]
              > >...and the best:
              > ><td class=\"arial1\ ">$row[0]</td>\n";[/color]
              >
              > Mmm, not quite. The class should ideally unambiguously describe the
              > section of the document e.g class="product_ table_item"[/color]

              it is off-topic here, but, why on earth (if you like) not to describe
              some formating with css? assume you have some form and after validating
              data, you want to show that something is wrong with red background?
              (disgusting, but if someone like it...)

              --
              Jan ko?
              fotografija = zapisano svjetlom | fotozine = foto-e-zin

              --

              Comment

              • Geoff Berrow

                #8
                Re: When to use single or double quotes

                I noticed that Message-ID: <1gnhty5.aq87qf lfwscuN%not_me@ mail.dot> from
                JaNE contained the following:
                [color=blue][color=green]
                >> Mmm, not quite. The class should ideally unambiguously describe the
                >> section of the document e.g class="product_ table_item"[/color]
                >
                >it is off-topic here, but, why on earth (if you like) not to describe
                >some formating with css? assume you have some form and after validating
                >data, you want to show that something is wrong with red background?
                >(disgusting, but if someone like it...)[/color]

                It is possible to define a lot of css classes to cover every eventuality
                arial_bold, arial_bold_red, arial_itallic_b old_red and so on. Then
                whenever you wanted to style something pick from the list. But it's a
                terrible idea. You would have no idea what type of elements these
                classes are being applied to and changing the look of a site would still
                involve editing every single site page!

                The idea of CSS is to promote consistency. Therefore it is better to
                name the classes after the parts of the document they cover, e.g.
                preamble, chapter_heading , chapter_subhead ing etc. Then if you decided
                to change the look of one particular element, simply change the CSS and
                it is changed throughout the site. If you want a high contrast version,
                simply change the CSS, no problem.

                See www.csszengarden.com


                --
                Geoff Berrow (put thecat out to email)
                It's only Usenet, no one dies.
                My opinions, not the committee's, mine.
                Simple RFDs http://www.ckdog.co.uk/rfdmaker/

                Comment

                • JaNE

                  #9
                  Re: When to use single or double quotes

                  Geoff Berrow <blthecat@ckdog .co.uk> wrote:
                  [color=blue]
                  > It is possible to define a lot of css classes to cover ... But it's a
                  > terrible idea. ...
                  > The idea of CSS is to promote consistency. ...
                  >[/color]

                  of course, of course, of course... reason why I wrote class=\"arial1\ "
                  wasn't suggestion to have specific class for each and every think on
                  page, just kind of description that he can create css class with
                  definition of font and size.
                  I don't like to use too meny classes or ids in my css file, depending on
                  kind of website, 10 to 20 basic definitions. of course, each can have
                  'subdefinitions ' (like "div#links a:hover") but that is another
                  storry...

                  --
                  Jan ko?
                  fotografija = zapisano svjetlom | fotozine = foto-e-zin

                  --

                  Comment

                  • Michael Fesser

                    #10
                    Re: When to use single or double quotes

                    .oO(JaNE)
                    [color=blue]
                    >...and the best:
                    ><td class=\"arial1\ ">$row[0]</td>\n";[/color]

                    Even better (no ugly escaping, single quotes are allowed in HTML):

                    echo "<td class='arial1'> $row[0]</td>\n";

                    Micha

                    Comment

                    Working...