0

If I write something like

$var = 12;
echo '$var'; //$var gets printed
echo "$var"; //12 gets printed

But when I do something like

$name = 'peter'; $email = 'peter@yahoo.com';
$query = "INSERT INTO email_list (name, email)  VALUES ('$name', '$email')";

Why are the values of the variables getting inserted in the table? Why do '$name' and '$email' not get inserted into the table, since they are enclosed inside single quotes?

Similarly,

echo 'i am going <br />';

is not printing: i am going <br />

Jason
  • 15,017
  • 23
  • 85
  • 116
Akash Goel
  • 161
  • 10
  • 1
    You should just read what the [documentation](http://www.php.net/manual/en/language.types.string.php) has to say about it. – crush Jan 10 '14 at 20:41
  • And, it is printing `i am going
    `. You probably don't see the `
    ` because it is rendered as HTML by the browser. Change the `Content-type` header to `text/plain` or escape the angle brackets if you want `
    ` to appear on the page lexically.
    – crush Jan 10 '14 at 20:42

3 Answers3

3

Why the values of the variables are getting inserted in the table, why not '$name' and '$email' are getting inserted in the table, since they are enclosed inside single quotes.

Because your string is quoted with double quotes ("INSERT INTO email_list (name, email) VALUES ('$name', '$email')";), the entire string has variable expansion performed. If you reversed your use of quotes, then you would get the output you're referring to with $name and $email in the string.

Similarly, echo 'i am going <br />'; Why its not printing: i am going <br />

Well it reality it does, but <br /> is just a line break and produces no visible output, just a line break. To output a tag like <br /> and see it, you'd need to output the entity instead, for example &lt;br /&gt; which renders as <br /> in a browser.

For further reading, see http://www.php.net/manual/en/language.types.string.php#language.types.string.parsing

j08691
  • 204,283
  • 31
  • 260
  • 272
2

double quotes forces PHP to evaluate the string (even though it might not be needed), whereas string between single quotes is not evaluated. Here is the full description:

What is the difference between single-quoted and double-quoted strings in PHP?

Community
  • 1
  • 1
Awlad Liton
  • 9,366
  • 2
  • 27
  • 53
0

ok, because the single quotes are within the double quotes, they are acting as simply single quote characters. They're not acting as delimiters. ie:

"hey guys, I'm going to the movies!" // <-- acting as simple text
"hey guys".' '."eat my food, ok?" // <-- acting as delimiters since it's broken up into three actual strings concatenated by the dot operator.

does that make sense?

CreationTribe
  • 562
  • 1
  • 5
  • 17
  • you're last string 'i am going
    ' should print the html tag. If it's not, you're probably viewing it through a browser. Check the source for your output.
    – CreationTribe Jan 10 '14 at 20:46
  • so what you meant is single quotes are getting escaped inside the double quotes.....am i right – Akash Goel Jan 10 '14 at 20:48
  • Thanks crush, I'm actually not new to stackoverflow, however the account is. My last account was hooked to my last job - so, it kinda went the way of the wind. But thanks nonetheless :) – CreationTribe Jan 10 '14 at 20:48
  • @CreationTribe i'm viewing it in browser – Akash Goel Jan 10 '14 at 20:49
  • no, not escaped, they're simply within the double quote delimiters, so they're plain text with no function. You could also put double quotes within a single quote string. ' here " are " double " quotes ' If you have something like this: "hey, here's a quote \", nice isn't it?" there, you have to escape it so it doesn't close the string. – CreationTribe Jan 10 '14 at 20:51
  • Akash Goel, if you're viewing it in your browser, the html IS being echoed, however you cannot see it since the browser is rendering it. – CreationTribe Jan 10 '14 at 20:53