469,299 Members | 2,050 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,299 developers. It's quick & easy.

why is php so stupid?

ok, why is php so stupid?

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


but single quotes here:

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

double quotes here:

echo "$sid";

and single quotes here:

mysql_query("INSERT INTO `current_session` (SID, ID) VALUES('$sid', '$id')") or die(mysql_error());

Apr 5 '11 #1
4 3593
12,516 Expert Mod 8TB
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.
Apr 5 '11 #2
variable expansion?

it seems i have a lot to learn.
Apr 5 '11 #3
Aimee Bailey
197 Expert 100+
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:

Expand|Select|Wrap|Line Numbers
  1. 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:

Expand|Select|Wrap|Line Numbers
  1. $a = $_SESSION['name'];
  2. 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.

Apr 6 '11 #4
code green
1,726 Expert 1GB
When you call PHP stupid - to what are you comparing?
Apr 6 '11 #5

Post your reply

Sign in to post your reply or Sign up for a free account.

By using this site, you agree to our Privacy Policy and Terms of Use.