single quotes, double quotes and "undefined index"

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

    single quotes, double quotes and "undefined index"

    I am trying to write clean code but keep having trouble deciding
    when to quote an array index and when not to.

    sometimes when I quote an array index inside of double quotes I
    get an error about enased whitespace (to my best memory)

    AT other times I get an undefined index notice as below:

    Notice: Undefined index: last_reminder_i d in...

    the 2nd line (which wraps to the 3rd in this posting
    is the one listed in the error message:

    $sql = "SELECT * from notes
    where recall_date '0' and recall_date <= '$now' and id >
    '$_SESSION[last_reminder_i d]'";

    what is the correct syntax and/or can anyone point me at a
    reference that will explain this ?

    bill
  • ZeldorBlat

    #2
    Re: single quotes, double quotes and &quot;undefi ned index&quot;

    On Jul 13, 9:56 am, bill <nob...@spamcop .netwrote:
    I am trying to write clean code but keep having trouble deciding
    when to quote an array index and when not to.
    >
    sometimes when I quote an array index inside of double quotes I
    get an error about enased whitespace (to my best memory)
    >
    AT other times I get an undefined index notice as below:
    >
    Notice: Undefined index: last_reminder_i d in...
    >
    the 2nd line (which wraps to the 3rd in this posting
    is the one listed in the error message:
    >
    $sql = "SELECT * from notes
    where recall_date '0' and recall_date <= '$now' and id >
    '$_SESSION[last_reminder_i d]'";
    >
    what is the correct syntax and/or can anyone point me at a
    reference that will explain this ?
    >
    bill
    If you have an array called $foo with a key called "bar" the correct
    syntax is like this:

    $foo['bar'];

    or

    $foo["bar"];

    or

    $key = 'bar';
    $foo[$key];

    Comment

    • Captain Paralytic

      #3
      Re: single quotes, double quotes and &quot;undefi ned index&quot;

      On 13 Jul, 14:56, bill <nob...@spamcop .netwrote:
      I am trying to write clean code but keep having trouble deciding
      when to quote an array index and when not to.
      >
      sometimes when I quote an array index inside of double quotes I
      get an error about enased whitespace (to my best memory)
      >
      AT other times I get an undefined index notice as below:
      >
      Notice: Undefined index: last_reminder_i d in...
      >
      the 2nd line (which wraps to the 3rd in this posting
      is the one listed in the error message:
      >
      $sql = "SELECT * from notes
      where recall_date '0' and recall_date <= '$now' and id >
      '$_SESSION[last_reminder_i d]'";
      >
      what is the correct syntax and/or can anyone point me at a
      reference that will explain this ?
      >
      bill


      Comment

      • Michael Fesser

        #4
        Re: single quotes, double quotes and &quot;undefi ned index&quot;

        ..oO(bill)
        >I am trying to write clean code but keep having trouble deciding
        >when to quote an array index and when not to.
        Literal array indexs always have to be quoted, unless the entire array
        is embedded in double-quoted string without using curly syntax:

        print $foo['bar'];

        print "hello $foo[bar]";

        print "hello {$foo['bar']}";

        All correct.
        >sometimes when I quote an array index inside of double quotes I
        >get an error about enased whitespace (to my best memory)
        >
        >AT other times I get an undefined index notice as below:
        >
        >Notice: Undefined index: last_reminder_i d in...
        >
        >the 2nd line (which wraps to the 3rd in this posting
        is the one listed in the error message:
        >
        >$sql = "SELECT * from notes
        >where recall_date '0' and recall_date <= '$now' and id >
        >'$_SESSION[last_reminder_i d]'";
        Syntax is correct. I'm just wondering why the values for the ID and date
        fields are quoted. If a value is numeric, don't quote it. So the parser
        might be right and the index is just not defined?
        >what is the correct syntax and/or can anyone point me at a
        >reference that will explain this ?
        All possible syntax variants are explained in the manual (see the
        examples).

        Array do's and don'ts


        Micha

        Comment

        • bill

          #5
          Re: single quotes, double quotes and &quot;undefi ned index&quot;

          Michael Fesser wrote:
          .oO(bill)
          >
          >I am trying to write clean code but keep having trouble deciding
          >when to quote an array index and when not to.
          >
          Literal array indexs always have to be quoted, unless the entire array
          is embedded in double-quoted string without using curly syntax:
          >
          print $foo['bar'];
          >
          print "hello $foo[bar]";
          >
          print "hello {$foo['bar']}";
          >
          All correct.
          >
          >sometimes when I quote an array index inside of double quotes I
          >get an error about enased whitespace (to my best memory)
          >>
          >AT other times I get an undefined index notice as below:
          >>
          >Notice: Undefined index: last_reminder_i d in...
          >>
          >the 2nd line (which wraps to the 3rd in this posting
          > is the one listed in the error message:
          >>
          >$sql = "SELECT * from notes
          >where recall_date '0' and recall_date <= '$now' and id >
          >'$_SESSION[last_reminder_i d]'";
          >
          Syntax is correct. I'm just wondering why the values for the ID and date
          fields are quoted. If a value is numeric, don't quote it. So the parser
          might be right and the index is just not defined?
          I hate that: The index *is* undefined the first time through !

          I sure do appreciate the other pointers and will study them.
          >
          >what is the correct syntax and/or can anyone point me at a
          >reference that will explain this ?
          >
          All possible syntax variants are explained in the manual (see the
          examples).
          >
          Array do's and don'ts

          >
          Micha

          Comment

          • Markus

            #6
            Re: single quotes, double quotes and &quot;undefi ned index&quot;

            Michael Fesser schrieb:
            .oO(bill)
            >
            >I am trying to write clean code but keep having trouble deciding
            >when to quote an array index and when not to.
            >
            Literal array indexs always have to be quoted, unless the entire array
            is embedded in double-quoted string without using curly syntax:
            >
            print $foo['bar'];
            >
            print "hello $foo[bar]";
            >
            print "hello {$foo['bar']}";
            >
            All correct.
            Additionnally, if you want to be safe, you can just concatenate the string:
            $sql = "SELECT * FROM foo WHERE bar='".$_SESSIO N['bar']."'";

            Comment

            • Michael Fesser

              #7
              Re: single quotes, double quotes and &quot;undefi ned index&quot;

              ..oO(Markus)
              >Additionnall y, if you want to be safe, you can just concatenate the string:
              >$sql = "SELECT * FROM foo WHERE bar='".$_SESSIO N['bar']."'";
              Sure, but IMHO this kind of defeats the purpose of double-quoted strings
              in PHP. When I see something like

              print "text ".$var." more text ".$anotherV ar." and so on";

              or even worse

              print "<a href=\"".$url." \">".$text." </a>";

              then I'm getting...what' s that in English - goose bumps (de: Gänsehaut)?
              It's just terrible and hurts the eye of an experienced programmer. Not
              to mention that jumping in and out of string parsing and mixing it with
              escaping and different quote signs is very error-prone.

              Micha

              Comment

              • Michael Fesser

                #8
                Re: single quotes, double quotes and &quot;undefi ned index&quot;

                ..oO(bill)
                >Michael Fesser wrote:
                >
                >Syntax is correct. I'm just wondering why the values for the ID and date
                >fields are quoted. If a value is numeric, don't quote it. So the parser
                >might be right and the index is just not defined?
                >
                >I hate that: The index *is* undefined the first time through !
                If you know that an index will be undefined on the first run, then you
                have to take care of that in your scripts and use a default value if
                necessary. isset() exists.

                Micha

                Comment

                • Toby A Inkster

                  #9
                  Re: single quotes, double quotes and &quot;undefi ned index&quot;

                  Michael Fesser wrote:
                  print $foo['bar'];
                  print "hello $foo[bar]";
                  print "hello {$foo['bar']}";
                  >
                  All correct.
                  Correct, yes. But the middle example is unwise.

                  define('bar', 'baz');

                  --
                  Toby A Inkster BSc (Hons) ARCS
                  [Geek of HTML/SQL/Perl/PHP/Python/Apache/Linux]
                  [OS: Linux 2.6.12-12mdksmp, up 24 days, 18:03.]

                  demiblog 0.2.0 Released

                  Comment

                  • Michael Fesser

                    #10
                    Re: single quotes, double quotes and &quot;undefi ned index&quot;

                    ..oO(Toby A Inkster)
                    >Michael Fesser wrote:
                    >
                    >print $foo['bar'];
                    >print "hello $foo[bar]";
                    >print "hello {$foo['bar']}";
                    >>
                    >All correct.
                    >
                    >Correct, yes. But the middle example is unwise.
                    >
                    >define('bar' , 'baz');
                    Didn't test it, but according to the manual it should work:

                    | // The following is okay as it's inside a string. Constants are not
                    | // looked for within strings so no E_NOTICE error here
                    | print "Hello $arr[fruit]"; // Hello apple

                    Micha

                    Comment

                    • Toby A Inkster

                      #11
                      Re: single quotes, double quotes and &quot;undefi ned index&quot;

                      Michael Fesser wrote:
                      Didn't test it, but according to the manual it should work
                      It will work, yes, but it's unwise, as you'd be skating on very thin ice.

                      echo $foo[bar];
                      echo "{$foo[bar]}";

                      will both use the constant 'bar' instead of the string 'bar' if it exists.
                      It's very easy to slip up in this area, so safer to always quote strings
                      used as array indices.

                      PHP is known to change its syntax from time to time, and may well become
                      less forgiving in the future with regard to something like:

                      echo "$foo[bar]";

                      and I'd imagine such a bug may be very tricky to track down.

                      --
                      Toby A Inkster BSc (Hons) ARCS
                      [Geek of HTML/SQL/Perl/PHP/Python/Apache/Linux]
                      [OS: Linux 2.6.12-12mdksmp, up 24 days, 23:34.]

                      demiblog 0.2.0 Released

                      Comment

                      • Markus

                        #12
                        Re: single quotes, double quotes and &quot;undefi ned index&quot;

                        Michael Fesser schrieb:
                        .oO(Markus)
                        >
                        >Additionnall y, if you want to be safe, you can just concatenate the string:
                        >$sql = "SELECT * FROM foo WHERE bar='".$_SESSIO N['bar']."'";
                        >
                        Sure, but IMHO this kind of defeats the purpose of double-quoted strings
                        in PHP. When I see something like
                        >
                        print "text ".$var." more text ".$anotherV ar." and so on";
                        >
                        or even worse
                        >
                        print "<a href=\"".$url." \">".$text." </a>";
                        >
                        then I'm getting...what' s that in English - goose bumps (de: Gänsehaut)?
                        It's just terrible and hurts the eye of an experienced programmer. Not
                        to mention that jumping in and out of string parsing and mixing it with
                        escaping and different quote signs is very error-prone.
                        I partially agree. Anyway I see nothing Gänsehaut-prone about
                        concatenating single-quoted strings, such as

                        print 'text '.$var.' more text '.$arr['foo'].' '.$obj->bar.'st time';

                        It is IMO a good habit if somebody does not want to run into the
                        questions discussed above, and there seem to be even performance reasons
                        for it:


                        But I admit, the above $sql example with double quotes originates in my
                        early insecureness about if double quotes would work inside an SQL
                        query, so I got used to double-quote those and use single quotes inside.

                        Comment

                        • Toby A Inkster

                          #13
                          Re: single quotes, double quotes and &quot;undefi ned index&quot;

                          Markus wrote:
                          print 'text '.$var.' more text '.$arr['foo'].' '.$obj->bar.'st time';
                          printf( 'text %s more text %s %d time',
                          $var, $arr['foo'], ordinal($obj->bar));

                          function ordinal ($number, $lang='en')
                          {
                          $number = (int)$number;

                          if ($lang=='en')
                          {
                          if ($number==11) return '11th';
                          if ($number==12) return '12th';
                          if ($number==13) return '13th';

                          switch ($number % 10)
                          {
                          case 1: return "{$number}s t";
                          case 2: return "{$number}n d";
                          case 3: return "{$number}r d";
                          default: return "{$number}t h";
                          }
                          }
                          else
                          return $number;
                          }

                          --
                          Toby A Inkster BSc (Hons) ARCS
                          [Geek of HTML/SQL/Perl/PHP/Python/Apache/Linux]
                          [OS: Linux 2.6.12-12mdksmp, up 25 days, 17:56.]

                          demiblog 0.2.0 Released

                          Comment

                          • Toby A Inkster

                            #14
                            Re: single quotes, double quotes and &quot;undefi ned index&quot;

                            Toby A Inkster wrote:
                            printf( 'text %s more text %s %d time',
                            $var, $arr['foo'], ordinal($obj->bar));
                            Oops -- my finger slipped.

                            s/d time/s time/;

                            --
                            Toby A Inkster BSc (Hons) ARCS
                            [Geek of HTML/SQL/Perl/PHP/Python/Apache/Linux]
                            [OS: Linux 2.6.12-12mdksmp, up 25 days, 20:03.]

                            demiblog 0.2.0 Released

                            Comment

                            • Andy Jeffries

                              #15
                              Re: single quotes, double quotes and &quot;undefi ned index&quot;

                              On Mon, 16 Jul 2007 10:01:50 +0200, Markus wrote:
                              I partially agree. Anyway I see nothing Gänsehaut-prone about
                              concatenating single-quoted strings, such as
                              >
                              print 'text '.$var.' more text '.$arr['foo'].' '.$obj->bar.'st time';
                              >
                              It is IMO a good habit if somebody does not want to run into the
                              questions discussed above, and there seem to be even performance reasons
                              for it:
                              Assuming it is slower to use variables within strings/double quotes* -
                              it comes down to the argument of developer time/cost versus server/CPU
                              cycles cost.

                              Personally I find double quotes with embedded variables to be MUCH more
                              readable and therefore a 0.1 second difference over a million prints is
                              negligible (particularly in comparison to the delay while waiting for the
                              DB servers to respond).

                              * The next comment after the one you linked suggests that it's faster to
                              do string interpolation than concatenation with either single or double
                              quotes:



                              Cheers,


                              Andy

                              Comment

                              Working...