Hi all:

I have gotten into the habit of writing array elements like so:
$array[key] rather than $array['key']
particularly on superglobals $_POST[field] as it seems easier to include them in SELECT statements, etc.

I have read that it is "good form" to use the single quotes. Why is that? Does it really matter? Am I being too sloppy?

Thanks

    Yes, it really does matter.

    Read the PHP Manual entry on arrays, in particular the section entitled "Why is $foo[bar] wrong?" in "Array do's and don'ts".

      Wow, I should RTFM. It actually made some sense to me, but I got a little lost...

      So this is correct:

      if ($_GET['action']=='approve' && isset($_GET['uid'])) {
          $user_approval_query = "UPDATE users SET approved=1 WHERE user_id = '$_GET[uid]'";

      but if it is $_GET[$field] I would NEVER single quote it, right?

      Ok, so when do you use curly braces around it? Should you just do it all of the time? No, because this would throw and error, right?

      if ({$_GET['action']}=='approve' && isset({$_GET['uid']}))

      but this would be ok, or even better?

      $user_approval_query = "UPDATE users SET approved=1 WHERE user_id = '{$_GET[uid]}'"; 

      😕

        Use the braces within double quote delimited strings, e.g.
        $foo = "example {$_REQUEST['ed']} of a string";

        You still need to quote the associative index here:

        $user_approval_query = "UPDATE users SET approved=1 WHERE user_id = '{$_GET['uid']}'";

          Correct, you wouldn't want single or double quotes in this case: $GET[$field] - $field is a variable which specifies the desired element in $GET[]. If there was an element in $GET named "$field" (which would be rather confusing), then you'd do $GET['$field']. I've seen code where the $field is wrapped in double quotes. The end result shoudl still be: $GET["$field"] == $GET[$field]. The difference being the double quotes makes PHP do a lot more work and has you doing more typing.

          $user_approval_query = "UPDATE users SET approved=1 WHERE user_id = '{$_GET['uid']}'";
          

          I think works (although you're missing the single quotes around uid), but that to me, is an ugly mess. I think this might be a ton easier to read and troubleshoot:

          $user_approval_query = "UPDATE users SET approved=1 WHERE user_id = '" . $_GET['uid'] . "'";
          

            ok, so in short, always single quote the index unless it is a defined constant, and always surround the array['name'] with curly braces inside a double quoted string (and heredoc, I would presume). Is that right?

            Thanks laserlight, anyone tell you that you resemble a walking manual ?😉

              Thanks Astro, if you are going to concentate (I am sure I spelled that wrong) isn't it faster to use single quotes since PHP has to look for inerpolations on doubles?

               $user_approval_query = 'UPDATE users SET approved=1 WHERE user_id = \'' . {$_GET['uid']} . '\''; 

              But I guess that is really fugly.

                Originally posted by travelbuff
                Thanks Astro, if you are going to concentate (I am sure I spelled that wrong) isn't it faster to use single quotes since PHP has to look for inerpolations on doubles?

                Not really; when I did some (pretty simple) time trials double-quoted strings were just as fast as single-quoted strings except when the double-quoted strings really did have variables in them - then the double-quoted strings came out slower (with the ratio depending on PHP version).

                  Originally posted by Weedpacket
                  Not really; when I did some (pretty simple) time trials double-quoted strings were just as fast as single-quoted strings except when the double-quoted strings really did have variables in them - then the double-quoted strings came out slower (with the ratio depending on PHP version).

                  That's cool to know.

                  Travelbuff: Just remember to escape your single quotes. I just noticed the board mucks with the backslash and single quote.

                    Write a Reply...