why is php so stupid?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • makai
    New Member
    • Apr 2011
    • 4

    why is php so stupid?

    ok, why is php so stupid?

    why do i have to put double quotes inside the brackets here:

    echo $_SESSION["PHPSESSID"];

    but single quotes here:

    $_SESSION['id'] = $row['FNAME'];

    double quotes here:

    echo "$sid";

    and single quotes here:

    mysql_query("IN SERT INTO `current_sessio n` (SID, ID) VALUES('$sid', '$id')") or die(mysql_error ());


    ????????????
  • Rabbit
    Recognized Expert MVP
    • Jan 2007
    • 12517

    #2
    It shouldn't matter if you use double quotes or single quotes for the first two examples.

    Double quotes are required for the third example because you want to expand the variable. Single quotes will not expand a variable and will instead treat the string as a literal. That way, you don't have to deal with the readability issues of escape characters.

    For the last example, you could use single quotes but then you would have to escape the single quotes within the string because mysql requires single quotes.

    So no, single quote or double quote doesn't matter as long as we're not talking about variable expansion. In the case of variable expansion, double quotes are necessary. But in every other case, it doesn't matter what you use as long as you escape the quotes you're using if they're within the string.

    Comment

    • makai
      New Member
      • Apr 2011
      • 4

      #3
      literal?
      escape?
      variable expansion?

      it seems i have a lot to learn.

      Comment

      • Aimee Bailey
        Recognized Expert New Member
        • Apr 2010
        • 197

        #4
        Essentially what Rabbit was saying, was that there is not much different between single and double quotes. However each has it's own benefit.

        As a starting rule, use single quotes when nothing fancy is going on between them. for instance:

        Code:
        echo 'hello, how are you today?';
        And double quotes when you want to do something that involves mixing the string with variables or escape characters. Like so:

        Code:
        $a = $_SESSION['name'];
        echo "Hello $a\n How are you today?";
        Note that \n is an escape character, this one in particular being an escape character that causes a line-break. Also, notice that I used singles instead of doubles for 'name', as nothing fancy was going on there.

        Aimee.

        Comment

        • code green
          Recognized Expert Top Contributor
          • Mar 2007
          • 1726

          #5
          When you call PHP stupid - to what are you comparing?

          Comment

          Working...