New to PHP so I've got a question. As I'm going through my book (PHP5 and MySQL Bible 2004), they seem to alternate between single quotes and double quotes. Sometimes I understand because the single quotes are literal, yada-yada... But sometimes I don't understand why, the most perplexing example is when they do it around an array.

Can anyone tell me what the difference is between the following

  1. $_POST['$variable']
  2. $_POST["$variable"]

They both seem to work, but I can't find any explanation as to why sometimes they use single and sometimes they use double. I don't want to wait and find out only after days of debugging a crashed program.

Thanks to all.

    single quotes will output the variable like this
    <?
    $name = 'loz';
    echo 'hi and welcome $name';

    #will output
    #hi and welcome $name

    echo "hi and welcome $name";
    will output
    hi and welcome loz

    ?>

    I hope this makes sense

      Read the PHP Manual concerning strings.

      In your example, the use of $_POST['$variable'] is probably incorrect.

        4 days later

        But WHY is the example below incorrect? Especially if it works?

        $_POST['$variable']

          I'm surprised it does work. Do you really have a form field named $variable?

            I user single quotes in my $POST all the time. Should I change over?

              I, too, use single qoutes. Primarily in array keys. I don't know why. I just do. I'm going to have to look into this as well, because I've never fully(or even partially) understood the practical differences between the two.

                There is nothing wrong with using single quotes to refer to array keys. The confusion here seems to be arising from the use of the $ symbol in front of the word variable.

                Some examples:
                #1 - [FONT=Courier New]$POST['variable'][/FONT] refers to the value corresponding to the key 'variable'
                #2 - [FONT=Courier New]$
                POST['$variable'][/FONT] refers to the value corresponding to the key '$variable'
                #3 - [FONT=Courier New]$POST["variable"][/FONT] is essentially the same as #1
                #4 - [FONT=Courier New]$
                POST["$variable"][/FONT] refers to the field with the same name as the VALUE of $variable, eg. if $variable = 'varname', then $_POST["$variable"] will refer to the value corresponding to the key 'varname';

                Another example:

                [FONT=Courier New]$data = array(
                'color' => 'red',
                'length' => 10,
                '$field' => 'value'
                );

                $field = 'length';

                echo $data['color'] . ' ';
                echo $data['$field'] . ' ';
                echo $data["$field"] . ' ';[/FONT]

                will print:
                [FONT=Courier New]red value 10[/FONT]

                The key difference between single and double quotes is that double quotes allow you to use the backslash to escape characters such as \r (carriage return), \n (newline), \t (tab), etc, and interpolate variables "Hello $firstname". Single quotes are simpler to use (and have less overhead) but don't have any of those fancy features.

                And as laserlight already mentioned, check out the strings page in the PHP Manual. It has a lot of useful information on the topic.

                Ryan

                  laserlight wrote:

                  Read the PHP Manual concerning strings.

                  In your example, the use of $_POST['$variable'] is probably incorrect.

                  you are crazy, I only use $POST['var_name']

                  It's very rare that my code has any " " unless working with SQL.

                    <?php
                    $emp_id = $_POST["emp_id"];

                    thats the way it will work ............
                    am i right weedpacket!!!

                      But do you use $_POST['$variable']?

                        as far as i know the correct way to use quotes and double quotes is:

                        double quotes when definig a funtion value, example:

                        echo " bla bla bla ";

                        and quotes can be used on the values and inside of them ofcourse betwen double quotes , example:

                        echo " <p align='center'> bla bla bla </p>" ;

                        thats when u use ' so u dont break the " 😛

                        hope that this helps :queasy:

                          Good grief!

                          The only difference is the any string in double quotes is parsed by PHP for varaible substituion. So say you have a variable called $name containing the name John:

                          'My name is $name' will resolve to: My name is $name

                          whereas:

                          "My name is $name" resolves to: My name is John

                          There is a side effect of this though. And that is that php demands more special characters are escaped in double quotes then when using single quotes.

                          For those keen on, or need, maximum efficiency in their scripts you should only use double quotes when you want PHP to sub.

                            Wow, Subwayman, well put. I am glad it is that simple. And what do you have to escape in double qoutes other that other double quotes?

                              Man, this dead horse keeps on riding!

                              However, $POST['$variable'] still only means give me the value in the $POST array for a form input field named "$variable"...aka <input type="text" name="$variable">.

                              $variable = "widget";

                              $_POST["$variable"] may give you the value for <input type="text" name="widget">. However, I only see a use for that if you have a group of, say, text inputs generated by a loop with an integer tacked on to the end (yes, there are probably more examples.) and you need to do this.

                              $variable = "widget";
                              for ($i = 1; $ <= 10; $i++)
                              {
                              $currentvalue = $_POST["$variable$i"];
                              }

                              The short of it is, for almost every case $_POST[$variable] is the proper code convention when you substitute a variable for an array key.

                                And what do you have to escape in double qoutes other that other double quotes?

                                Read what the PHP Manual has to say about strings. I posted the link in post #3 of this thread, more than a week ago.

                                  Write a Reply...