General Guidelines for Quotes ?

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

    General Guidelines for Quotes ?

    Hi guys,

    I haven't done that much research on this topic but it seems I can use
    either the single quotes or the double quotes. SInce I am so used to C(++) I
    prefer the double quotes and am wondering if they could possible be less
    efficient. Maybe I should get in the habit of using single quotes with Php
    instead ?

    I like my code to be consistent, so in two very similar circumstances I
    don't want one set of code using single quotes and the other double (I'm
    kinda anal, what can I say ?)

    Here are some simple and very common usages:

    $fullname="Sata n"; // regular assignment that may of may not get passed to
    function

    $match="/[0-9]/"; // same but will definitely get passed to a function
    preg_match($mat ch,$string);

    $formdata=$_POS T["formData"]; // for arrays

    I have lots of similar code all throughout my Php programs. So what do you
    think, should I be using the single of double quotes in those situations ? I
    want to get into a good habbit now so I can write some solid stuff that I
    don't have to go back and change.

    Take care,
    Cyrus


  • jrf[no]

    #2
    Re: General Guidelines for Quotes ?

    Cyrus D. wrote:[color=blue]
    > Hi guys,
    >
    > I haven't done that much research on this topic but it seems I can use
    > either the single quotes or the double quotes. SInce I am so used to C(++) I
    > prefer the double quotes and am wondering if they could possible be less
    > efficient. Maybe I should get in the habit of using single quotes with Php
    > instead ?
    >
    > I like my code to be consistent, so in two very similar circumstances I
    > don't want one set of code using single quotes and the other double (I'm
    > kinda anal, what can I say ?)
    >
    > Here are some simple and very common usages:
    >
    > $fullname="Sata n"; // regular assignment that may of may not get passed to
    > function
    >
    > $match="/[0-9]/"; // same but will definitely get passed to a function
    > preg_match($mat ch,$string);
    >
    > $formdata=$_POS T["formData"]; // for arrays
    >
    > I have lots of similar code all throughout my Php programs. So what do you
    > think, should I be using the single of double quotes in those situations ? I
    > want to get into a good habbit now so I can write some solid stuff that I
    > don't have to go back and change.
    >
    > Take care,
    > Cyrus
    >
    >[/color]

    Cyrus,

    Have a look at the php online manual, they give some good guidelines on
    when to use which and don't forget to look at HEREDOC style too.

    The short of it is:
    What's between single quotes will be interpreted literally, i.e. '*your
    name is $name*' will print literally *your name is $name* without
    evaluating $name to the value of the variable.
    What's between double quotes will be evaluated, so "*your name is
    $name*" would come out as for instance *your name is Cyrus*

    Still, with double quotes you'll have to escape double quotes (such as
    used in HTML), with single quotes, you don't have to.
    Within double quotes escape characters such as \n for new line to create
    readable code will be interpreted, within single quotes, those also
    won't be interpreted.

    So basically, don't be anal and use both.
    Generally I use single quotes if it's plain HTML which I want to add to
    a string and double quotes for anything that needs evaluating, i.e.:

    var $theHtml = '<H2 class="testclas s">This is just a plain header</H2>';
    $theHtml .= '<P>Now I add some more information, such as this link:';
    $theHtml .= "<A HREF=\"$basepat h/$filename\">a link</A></P>";

    I don't pretend that this is best practice or anything, but it works for me.

    Good luck,
    Juliette

    Comment

    • Tony Marston

      #3
      Re: General Guidelines for Quotes ?

      If a string contains a variable reference that needs to be replaced with the
      contents of that variable, then you MUST use double quotes, as in
      $var = 'sample';
      echo "This is a string which references $var";

      this will output: This is a string which references sample

      If you use single quotes, as in
      $var = 'sample';
      echo 'This is a string which references $var';

      this will output: This is a string which references $var

      If you enclose a string in double quotes and it does not contain any
      variables to be substituted then you are causing PHP to do more work than
      necessary. It may only be a minimal difference, but it all adds up.

      --
      Tony Marston

      This is Tony Marston's web site, containing personal information plus pages devoted to the Uniface 4GL development language, XML and XSL, PHP and MySQL, and a bit of COBOL





      "Cyrus D." <satan@invalid. org> wrote in message
      news:ohvdd.2935 5$YM4.8681336@n ews4.srv.hcvlny .cv.net...[color=blue]
      > Hi guys,
      >
      > I haven't done that much research on this topic but it seems I can use
      > either the single quotes or the double quotes. SInce I am so used to C(++)
      > I prefer the double quotes and am wondering if they could possible be less
      > efficient. Maybe I should get in the habit of using single quotes with Php
      > instead ?
      >
      > I like my code to be consistent, so in two very similar circumstances I
      > don't want one set of code using single quotes and the other double (I'm
      > kinda anal, what can I say ?)
      >
      > Here are some simple and very common usages:
      >
      > $fullname="Sata n"; // regular assignment that may of may not get passed to
      > function
      >
      > $match="/[0-9]/"; // same but will definitely get passed to a function
      > preg_match($mat ch,$string);
      >
      > $formdata=$_POS T["formData"]; // for arrays
      >
      > I have lots of similar code all throughout my Php programs. So what do you
      > think, should I be using the single of double quotes in those situations ?
      > I want to get into a good habbit now so I can write some solid stuff that
      > I don't have to go back and change.
      >
      > Take care,
      > Cyrus
      >
      >[/color]


      Comment

      • Pjotr Wedersteers

        #4
        Re: General Guidelines for Quotes ?

        Tony Marston wrote:[color=blue]
        > If a string contains a variable reference that needs to be replaced
        > with the contents of that variable, then you MUST use double quotes,
        > as in $var = 'sample';
        > echo "This is a string which references $var";
        >
        > this will output: This is a string which references sample
        >
        > If you use single quotes, as in
        > $var = 'sample';
        > echo 'This is a string which references $var';
        >
        > this will output: This is a string which references $var
        >
        > If you enclose a string in double quotes and it does not contain any
        > variables to be substituted then you are causing PHP to do more work
        > than necessary. It may only be a minimal difference, but it all adds
        > up.[/color]

        I tested your theory, and AFAIK using strings in double quotes WITHOUT
        substitution vars uses the same amount of time as the single quoted ones.
        Only when substitution _does_ take place the processing becomes
        significantly slower. At least, that's what I get from my server. PHP
        4.3.8/Apache 2

        So I'd say go ahead and use what you like best. I use single quotes for
        MySql queries, as it makes for an easier and nicer string and less risk.

        Rgds
        Pjotr




        Comment

        • Cyrus D.

          #5
          Re: General Guidelines for Quotes ?

          Thanks a lot for that information guys, it clears things up for me. I had no
          idea you can put a variable inside a string with double quotes and have its
          value come out. I have been using the dot (period) operator for that.

          Is either method more efficient than the other, like so:

          $strName='Satan ';

          echo "Welcome to my Web site $strName";

          or

          echo 'Welcome to my Web site ' . $strName;

          I like the first method because it's easier to read; but I'll use the later
          method if it's faster. Thanks again, I'm finally getting the hang of PHP
          because of all the helpful people at this newsgroup :)

          Take care,
          Cyrus


          Comment

          • Nikolai Chuvakhin

            #6
            Re: General Guidelines for Quotes ?

            "Cyrus D." <satan@invalid. org> wrote in message
            news:<3tydd.325 11$YM4.9214366@ news4.srv.hcvln y.cv.net>...[color=blue]
            >
            > Is either method more efficient than the other, like so:
            >
            > $strName='Satan ';
            > echo "Welcome to my Web site $strName";
            >
            > or
            >
            > echo 'Welcome to my Web site ' . $strName;[/color]

            Neither. The most efficient method would be,

            $strName='Satan ';
            echo 'Welcome to my Web site ', $strName;

            The difference is that in the first case the double-quoted string
            is parsed, in the second case, the single argument being passed
            to echo requires concatenation, in the third case, you simply pass
            two arguments to echo, so no string operation takes place.

            Cheers,
            NC

            Comment

            • Michael Fesser

              #7
              Re: General Guidelines for Quotes ?

              .oO("jrf[no]" <"jrf[no]"@[spam]jokeaday.net>)
              [color=blue]
              >$theHtml .= "<A HREF=\"$basepat h/$filename\">a link</A></P>";[/color]

              You can also use single-quotes in HTML, no need for escaping:

              ....
              $theHtml .= "<A HREF='$basepath/$filename'>a link</A></P>";

              Micha

              Comment

              Working...